Skip directly to content

Block visible for Specific Content Types OR Specific URL (Drupal 7)

on June 15th, 2012 at 9:59:23 AM

Looking for a snippet to configure a block visibility, after that none of these solutions work and fight a little bit with php errors like:

Notice: Trying to get property of non-object in eval() (line 8 of /var/www/drupal/modules/php/php.module(80) : eval()'d code).

I finally got to obtain a snippet that shows a block in a page if the path contains a given string OR if the page is a node of a given content type. I'm sure it could be improved, but for the moment it serves his purpose and doesn't generate any error.

<?php
 
// Check proper node type.
 
$node = menu_get_object();

  if (isset(
$node) && ($node->type == 'family' || $node->type == 'species')) {
      return
TRUE;
  }

 
// Pages to match on (mynotes or mynotes* - using newline character).
 
$pages = "mynotes\nmynotes*";
 
// Get current alias.
 
$path = drupal_get_path_alias($_GET['q']);
 
// Compare with the internal and path alias (if any).
 
$page_match = drupal_match_path($path, $pages);

  if (
$path != $_GET['q'] || $page_match) {

   
// Check if match with internal or alias.
   
if ($page_match || drupal_match_path($_GET['q'], $pages)) {
      return
TRUE;
    }
  }
 
  else {
    return
FALSE;
}

?>

Pass nid as argument in URL path when opening node add form in a ctools modal frame

on May 11th, 2012 at 7:21:49 AM

Here is the best tutorial I found in the internet to open a "node add" form in a Ctools modal frame (with Drupal 7): http://www.pashasolomon.com/blog/ctools-modalframe-node-creation-forms.

What we want to do is pass an argument through the Ctools modale frame URL path and inject it in a Entity/Node reference field. Before coding we need to install one of the following module: Entityreference prepopulate or Node Reference URL Widget. What both modules are doing is checking if a node NID is present in the URL path from your browser and if so, it will be passed to the form you are opening to prepopulate a field.
To make it appened, the module needs to get the node NID from an "add node" link you have in a custom block, for example. When you need that this link open in a Ctools modale frame, you'll probably have use the ctools_modal_text_button function.

From the aboved mentioned tutorial, in the sidebar_link() function we will change:

<?php
  $output
= ctools_modal_text_button(t('Submit an event'), 'create/nojs/public-event', 'public-event');
?>

by

<?php
  $arg1
= arg(1);
// get the node nid from the URL
  $link_modal = ctools_modal_text_button(t('Submit an event'), 'create/nojs/public-event?field_you_want_to_prepopulate='.$arg1, 'public-event');
 
$output = str_replace (array('%3F', '%3D'), array('?', '='), $link_modal);
?>

 

Reorder the node options vertical tabs in a Drupal 7 form

on February 10th, 2012 at 10:22:59 PM

I don't know if it's some module i use (Display Field, Renderable Element, Field Group) or the last Drupal core versions (http://drupal.org/node/467296), but it seems that it's not possible anymore to change the Vertical Tabs order of a node edit form. Most concretely, I would like to see the Publishing options tab displayed before the Menu settings or the Revision information ones, as it is much more important and useful that these two others I mention here as an example..

Fortunately a simple form alter in the template theme will help us to resolve the situation:

function fil3_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
  case 'node_type_form_ID'; // put here your form ID
    $form['additional_settings']['#weight'] = -100; // this one is the vertical tab named "Publishing options"
    $form['revision_information']['#weight'] = 99; // and this one is the "Revision information" one
    break;
  }
}

 

Add a custom close button to a BeautyTips with jQuery and Drupal 7

on February 2nd, 2012 at 12:37:50 PM

Sometimes a hovertip is not that easy to close if the user never dealed with them and doesn't guess that he can do it simply clicking anywhere out of the hovertip. So it's would be easier if we could add a simple "close" button to the whole thing, right ? Anyway that's what my client was arguing. Here is the sample jQuery code that will do the job for the BeautyTips module :

$('.bt-content').prepend("<div class='close-button'><img alt='' width='10' height='10' src='/sites/all/themes/yourthemename/images/closebox.gif' /></div>");
$('.close-button').attr('title', 'click here to close this dialog').click(function() {
    $(document).click();
});

The whole snippet has to be placed in a javascript file or, in case you don't want to build your own module or you don't know how to call a .js file within Drupal, the fastest solution is to use the JS Injector module. Here is the whole snippet that generates the hovertip with the close button :

(function($) {

    $(document).ready(function() {
        $('.beautytips').bt({ // .beautytips or whatever class name you need to use
            showTip: function(box) {
                $('.bt-content').prepend("<div class='close-button'><img alt='' width='10' height='10' src='/sites/all/themes/yourthemename/images/closebox.gif' /></div>");
                $('.close-button').attr('title', 'click here to close this dialog').click(function() {
                    $(document).click();
                });
                $(box).fadeIn(200);
            },
            hideTip: function(box, callback) {
                $(box).animate({
                    opacity: 0
                }, 200, callback);
            },

            contentSelector: "$('span.links-m-cloned-wrapper a')",
            positions: ['most'],
            fill: '#F4F4F4',
            strokeStyle: '#666666',
            spikeLength: 20,
            spikeGirth: 10,
            //width: 350,
            overlap: 0,
            centerPointY: 1,
            cornerRadius: 0,
            cssStyles: {
                fontFamily: '"Lucida Grande",Helvetica,Arial,Verdana,sans-serif',
                fontSize: '12px',
                padding: '10px 14px'
            },
            shadow: true,
            shadowColor: 'rgba(0,0,0,.5)',
            shadowBlur: 8,
            shadowOffsetX: 4,
            shadowOffsetY: 4,
            trigger: ['mouseover', 'click']
        });

    });
})(jQuery);

Others javascipt snippets examples to build the BeautyTips tooltip can be found here. Mine is based on the "Hulu style" one.
Optionnaly you should add this little piece of CSS code, which places the close button in the top-right position :

.bt-content .close-button {
    position: absolute;
    top: 0;
    right: 6px;
    cursor: pointer;
}

And here is the result :

How to use Fields to generate an ImageCache custom action (D7)

on January 19th, 2012 at 10:32:53 AM

Since January 2012, it's now possible to use D7 fields in a custom action, from the ImageCache Actions module. This improvement brings a lot of possibilities to setup complex image manipulation.

Let's look at the code... Let's say I have a content-type name Ilustration, that contains some fields. One of them is an ImageCache picture, and an other one is a Decimal field, that defines a percent number in order to scale the picture when the user is visiting the node view.

So, if I want to retrieve the percent value, the code I put will be :

<?php
foreach ($fields as $fid => $field_instance) {
  foreach (
$field_instance as $field_id => $entity_types) {
    foreach (
$entity_types as $entity_type => $entity_instances) {
      foreach (
$entity_instances as $entity_id => $entity_stub) {
       
$entity = entity_load_single($entity_type, $entity_id);
       
$percent_value  = $entity->field_illustration_percent[LANGUAGE_NONE][0]['value']; // my D7 CCK field
       
}
     }
   }
}


image_gd_resize($image,$image->info['width']*$percent_value,$image->info['height']*$percent_value);

return
$image;
?>

Source: http://drupal.org/node/1070262

How to display a custom sortable table for Drupal 7

on January 16th, 2012 at 2:14:11 PM

Here is a snippet for Drupal 7 that builds a custom table in a page. The columns can be sorted by clicking in their header label. For a Drupal 5 (and 6 ?) version of this snippet, see http://drupal.org/node/205347. The example is using the node table.

<?php
  $header
= array(
    array(
'data' => 'Title', 'field' => 'title', 'sort' => 'ASC'),
    array(
'data' => 'Node ID', 'field' => 'nid'),
    array(
'data' => 'Type', 'field' => 'type'),
    array(
'data' => 'Created', 'field' => 'created'),
    array(
'data' => 'Published', 'field' => 'status'),
    array(
'data' => 'Sticky', 'field' => 'sticky'),
    array(
'data' => 'Promoted', 'field' => 'promote'),
    );
 
$rows = array();
 
$noyes = array('No', 'Yes');

 
  // We select the 'node' table from the database.
 
$select = db_select('node', 'n')->extend('TableSort');
  // We select the fields we want to display as columns in the table
 
$results = $select->fields('n', array('nid', 'title', 'type', 'created', 'status', 'sticky', 'promote'))->orderByHeader($header)->execute();

  foreach (
$results as $node) {
   
$rows[] = array(
     
l($node->title, 'node/'. $node->nid .'/edit'),
     
$node->nid,
     
$node->type,
     
format_date($node->created),
     
$noyes[$node->status],
     
$noyes[$node->sticky],
     
$noyes[$node->promote],
      );
   }

  return
theme('table', array(
   
'header' => $header,
   
'rows' => $rows,
   
'id' => 'table-duplicates', // an optional ID attribute for the table
   
'class' => 'duplicates',
// an optional class for the table
  ));
?>
 
This code can be inserted directly in a page, with the PHP Code text format. Here is a preview of the result, based on a Biblio module table :
 

Add the general RSS feed when using Panel as a homepage

on June 3rd, 2011 at 12:02:26 PM

I just add a new custom content in a pane from my homepage panel with:

<?php
drupal_add_feed
('rss.xml', 'General RSS feed');
?>

The PHP snippet put a RSS feed icon in the page and activate the web browser rss icon too.

A few must-read drupal 7 developer 's links

on May 12th, 2011 at 8:23:57 AM

Drupal Entities - Part 1 - Moving beyond nodes
http://www.istos.it/blog/drupal/drupal-entities-part-1-moving-beyond-nodes

Mora than 50 Drupal [contrib] modules moved into Drupal [core] 7
http://www.unleashedmind.com/en/blog/sun/more-than-50-drupal-modules-mov...

Drupal 7 API navigation
http://api.drupal.org/api/drupal/7

Taxonomy & Drupal 7
http://drupal.org/node/1107028

Upgrading Drupal 6 to Drupal 7
http://www.thankgnu.com/content/upgrading-drupal-6-drupal-7

Converting 6.x modules to 7.x
http://drupal.org/update/modules/6/7

New books to read: Drupal's Building Blocks / Pro Drupal 7 Development

on February 6th, 2011 at 4:58:59 PM

Now that Amazon.co.uk is offering free delivery to Spain, even the price of books is not an excuse anymore... These two pieces of literature have just arrived home.

Drupal's Building Blocks

Tags: 

Display Views exposed filters in a block

on March 26th, 2010 at 8:46:13 AM

block pic
When the "Exposed form in block" option from the Views UI doesn't work, here is a PHP snippet you can put in a new custom block in order to obtain the exact same thing:

<?php
$view=
views_get_view('name_of_your_view');
  if(
$view){
   
$view->set_display('name_of_your_view_display');
   
$view->init_handlers();
   
$form_state = array(
     
'view' => $view,
     
'display' => $view->display_handler->display,
     
'exposed_form_plugin' => $view->display_handler->get_plugin('exposed_form'),
     
'method' => 'get',
     
'rerender' => TRUE,
     
'no_redirect' => TRUE,
    );
   
$output = drupal_build_form('views_exposed_form', $form_state);
  }else{
   
$output = ""; // error messages or something...
 
}
  print
$output;
?>

Source: http://drupal.org/node/290691 and https://drupal.org/node/876908