Category Archives: drupal

Drupal: Cross-Linking Custom Content-Types via Custom Fields using Entity API and Field API

Scenario: a book where pages have multiple subpages as tabs, e.g. a Drupal Book content-type with documentation of RESTful API Resources, where each API Resource Book page is linked to multiple Code Example content-types, ordered in a tabbed page with one Code Example per language tab.

drupal_crosslinks_entity_reference

Drupal 7 and 8 has a set of abstraction layers, abstracting nodes to Entity Type, Bundle, and Field. A Node is an Entity Type, just like Taxonomy, Comment or User, or Book, Article, Blog Post etc. An Entity is an implementation instance of an Entity Type. An Entity Type is defined in a Bundle that has a collection of Fields.

Create a new ‘Entity Type’ named ‘Code Example’. Install the Entity Reference module. On the Book page, click Manage Fields, add a Field to the Book page Entity Type of type Entity Reference with multiple selections. Create a few Code Examples for the Book page node. On the instance or node of the Book page, select the Entity References of type Code Examples.

Create a Taxonomy with a hierarchy of coding languages. In the Code Example entity type, click Manage Fields and add a field based on the coding languages taxonomy. The tabs in the Book page will be labeled by code language, e.g. Java, JavaScript, Objective-C, etc.

Then, to use JQuery UI.tabs we can create a tabular page with Code Examples per language, crosslinked to entities or nodes of type Book Page.

<?php
drupal_add_library('system', 'ui.tabs');
drupal_add_js('jQuery(document).ready(function(){jQuery("#codeexamples-tabs").tabs();});', 'inline');
?>
<div id="codeexamples-tabs">
<?php
$node = menu_get_object();
if($node){
 $field = field_get_items('node', $node, 'field_codeexampleentityreference');
 if($field){
  $listtabs = '<ul>';
  $tabcontents = '';
  foreach ($field as $codeexampleentityreference)
  {
    $nid = $codeexampleentityreference['target_id'];
    $tabname = $nid;
    $codeexample = node_load($nid);
    $codeexample_output = node_view($codeexample);
    $field2 = field_get_items('node', $codeexample, 'field_language');
    if($field2){
     $tabname = $field2[0]["taxonomy_term"]->name;
    }
    $listtabs = $listtabs . '<li><a href="#codeexamples-tab-'.$nid.'">'.$tabname.'</a></li>';
    $tabcontents = $tabcontents . '<div id="codeexamples-tab-'.$nid.'">'.render($codeexample_output).'</div>';
   }
   $listtabs = $listtabs.'</ul>';
   print $listtabs;
   print $tabcontents;
  }
 }
?>
</div><!-- /.codeexamples-tabs -->

Drupal: template.php form_alter and breadcrumb hooks

Add a Read-Only field to User Register Form

To create a read-only field on the user registration form in Drupal, for instance to add a Terms & Conditions textarea field, you must override the form_alter hook.

function _form_alter(&$form, &$form_state, $form_id) {
 if ($form_id == 'user_register_form') {
  $form['field_developer_agreement']['und'][0]['value']['#attributes']['readonly'] = 'readonly';
  $form['field_developer_agreement']['und'][0]['value']['#resizable'] = FALSE;
  $form['field_developer_agreement']['und'][0]['value']['#cols'] = 40;
 }
}

Continue reading

Drupal: Color.Module

Tips and Tricks
When you get an ‘undefined index’ warning, make sure the mentioned key is present in your color.inc info array.

Important: Only if the color in your $info[‘css’] file that you want to change with the palette in the drupal color picker is preset in the colors.inc default scheme and the hex value exactly matches the hex value in the css file, will the color.module overwrite your css file settings.

Note: when testing and making changes to your regular colors.css file, you must regenerate the color.module’s copy of the colors.css file before you will see the changes, as color.module will look for the generated $info[‘css’] and ignore your original css.
Continue reading