Edit

Nodes

Items

86

Per Page

/ 5

Subject Attribute

  • Reading/setting Drupal config variables

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    function organic_context_config_page(){
        $form = array();
        $form['onthisdate_maxdisp'] = array(
            '#type' => 'textfield',
            '#title' => t('Maximum number of links'),
            '#default_value' => variable_get('onthisdate_maxdisp', 3),
            '#size' => 2,
            '#maxlength' => 2,
            '#description' => t("The maximum number of links to display in the block."),
            '#required' => TRUE,
        );
        return system_settings_form($form);
    }
    ?>
  • Migrates multigroups in D6 to field collections in D7

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    
    /////////////////// PAGE DEFINITIONS ///////////////////
    
    
    function migrate_groups_menu(){
        $menu["migrate_groups"] = array(
            "title" => "Migrate grps",
            "description" => "....",
            "page callback" => "migrate_multigroup_to_collections",
            "access callback" => TRUE,
        );
        return $menu;
    }
    
    
    
    /*function migrate_multigroup_to_collections() {
      $content_type = 'thing';
      $collection_field = 'field_context';
      $multigroup_fields = array(
        'field_connection',
        'field_title_in_context',
      );
    
      // Get all the nodes that have value in the multigroup.
      $query = db_select('field_data_' . $multigroup_fields[0])
        ->condition('entity_type', 'node')
        ->condition('bundle', $content_type);
      $query->addExpression('DISTINCT entity_id', 'nid');
      $query->addExpression('revision_id', 'vid');
      $nodes_result = $query->execute();
    
      foreach ($nodes_result as $node) {
        // Construct the legacy multigroup for the node from the individual fields.
        $multigroup_data = array();
        foreach ($multigroup_fields as $field) {
          $field_result = db_select('field_data_' . $field, 'field')
            ->fields('field')
            ->condition('entity_type', 'node')
            ->condition('entity_id', $node->nid)
            ->execute();
    
          foreach ($field_result as $field_item) {
            $multigroup_data[$field_item->delta][$field] = $field_item;
          }
    
        }
    
        // Step through the reconstructed multigroups, which are collections from now.
        foreach ($multigroup_data as $delta => $data) {
          // Create entry in field_collection_item table.
          $id = db_insert('field_collection_item')
            ->fields(array('field_name' => $collection_field))
            ->execute();
    
          // Attach collection field data to the node.
          db_insert('field_data_' . $collection_field)
            ->fields(array(
              'entity_type' => 'node',
              'bundle' => $content_type,
              'entity_id' => $node->nid,
              'revision_id' => $node->vid,
              'language' => 'und',
              'delta' => $delta,
              $collection_field . '_value' => $id,
            ))
            ->execute();
    
          // Go through all the fields in the multigroup.
          foreach ($data as $multigroup_field => $field_data) {
            // Reassign the fields in the multigroup from the node to the collection field instance.
            db_update('field_data_' . $multigroup_field)
              ->fields(array(
                'entity_type' => 'field_collection_item',
                'bundle' => $collection_field,
                'entity_id' => $id,
                'revision_id' => $id,
                'delta' => 0,
              ))
              ->condition('entity_type', 'node')
              ->condition('entity_id', $node->nid)
              ->condition('delta', $delta)
              ->execute();
    
          }
        }
      }
      return "FINISHED";
    }*/
    
    
    
    function migrate_multigroup_to_collections($content_type='page',$collection_field='field_context',$multigroup_fields=array('field_connection','field_weight','field_notes')) {
    
      // Get all the nodes that have value in the multigroup.
      $query = db_select('field_data_' . $multigroup_fields[0])
        ->condition('entity_type', 'node')
        ->condition('bundle', $content_type);
      $query->addExpression('DISTINCT entity_id', 'nid');
      $query->addExpression('revision_id', 'vid');
      $nodes_result = $query->execute();
    
      foreach ($nodes_result as $node) {
        // Construct the legacy multigroup for the node from the individual fields.
        $multigroup_data = array();
        foreach ($multigroup_fields as $field) {
          $field_result = db_select('field_data_' . $field, 'field')
            ->fields('field')
            ->condition('entity_type', 'node')
            ->condition('entity_id', $node->nid)
            ->execute();
    
          foreach ($field_result as $field_item) {
            $multigroup_data[$field_item->delta][$field] = $field_item;
          }
    
        }
    
        $id = 0;
        // Step through the reconstructed multigroups, which are collections from now.
        foreach ($multigroup_data as $delta => $data) {
          // Create entry in field_collection_item table.
          $id = db_insert('field_collection_item')
            ->fields(array('field_name' => $collection_field, 'revision_id' => 0, 'archived' => 0))
            ->execute();
    
          $revid = db_insert('field_collection_item_revision')
            ->fields(array('item_id' => $id))
            ->execute();
    
          db_update('field_collection_item')
              ->fields(array('revision_id' => $revid))
              ->condition('item_id', $id)
              ->execute();
    
    
          // Attach collection field data to the node.
          db_insert('field_data_' . $collection_field)
            ->fields(array(
              'entity_type' => 'node',
              'bundle' => $content_type,
              'entity_id' => $node->nid,
              'revision_id' => $node->vid,
              'language' => 'und',
              'delta' => $delta,
              $collection_field . '_value' => $id,
              $collection_field . '_revision_id' => $revid,
            ))
            ->execute();
    
          // Attach collection field data to the node.
          db_insert('field_revision_' . $collection_field)
            ->fields(array(
              'entity_type' => 'node',
              'bundle' => $content_type,
              'entity_id' => $node->nid,
              'revision_id' => $node->vid,
              'language' => 'und',
              'delta' => $delta,
              $collection_field . '_value' => $id,
              $collection_field . '_revision_id' => $revid,
            ))
            ->execute();
    
          // Go through all the fields in the multigroup.
          foreach ($data as $multigroup_field => $field_data) {
            // Reassign the fields in the multigroup from the node to the collection field instance.
            db_update('field_data_' . $multigroup_field)
              ->fields(array(
                'entity_type' => 'field_collection_item',
                'bundle' => $collection_field,
                'entity_id' => $id,
                'language' => 'und',
                'revision_id' => $revid,
                'delta' => 0,
              ))
              ->condition('entity_type', 'node')
              ->condition('entity_id', $node->nid)
              ->condition('delta', $delta)
              ->execute();
    
           db_delete('field_revision_' . $multigroup_field)
              ->condition('entity_type', 'node')
              ->condition('entity_id', $node->nid)
            ->execute();
          $qry = "INSERT INTO field_revision_$multigroup_field SELECT * FROM field_data_$multigroup_field WHERE entity_id = $id AND entity_type = 'field_collection_item' AND bundle = '$collection_field'";
          db_query($qry);
    
          }
        }
      }
      return "DONE";
    }
    
    ?>
  • Drupal DB Query to find Children (Recursively)

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    function loadchildrens($context){
    	$sql_children = "SELECT node.nid AS nid,
       node.vid AS node_vid,
       node.title AS node_title
     FROM node node 
     LEFT JOIN content_field_connection node_data_field_connection ON node.vid = node_data_field_connection.vid
     WHERE (node.type in ('category')) AND (node_data_field_connection.field_connection_nid = ".$context.")
       ORDER BY node_title ASC";
    
    	$db_query_children = db_query( $sql_children );
    
    	while( $row = db_fetch_array($db_query_children) ){
    		$context_tree[ $row["nid"] ] = array();
    		if( $row["nid"] != "517" )
    			$context_tree[ $row["nid"] ] = loadchildrens( $row["nid"] );
    	}
    	return $context_tree;
    }
    ?>
    <?php
    
    $node = node_load(arg(1));
    
    $spheres_via_path = array();
    
    $sql_spheres_via_project = "SELECT node.title AS node_title, node.nid AS nid, node.type AS node_type
    FROM {node} node
    LEFT JOIN {field_data_field_project} field_data_field_project ON node.nid = field_data_field_project.entity_id
    WHERE (node.type in ('note')) AND (field_data_field_project.field_project_nid = '".arg(1)."')";
    
    $db_query_spheres_via_project = db_query( $sql_spheres_via_project );
    
    if( $db_query_spheres_via_project->rowCount() ){
    	$return = "<h3>Notes</h3>\n";
    	$return .= "<ul>\n";
    	foreach( $db_query_spheres_via_project as $sphere ){
    		if( isset($sphere->nid) ) $spheres_via_project[ $sphere->nid ] = $sphere;
    		$return .= "<li><a href=\"/node/".$sphere->nid."\">".$sphere->node_title."</a></li>\n";
    	}
    	$return .= "</ul>\n";
        return $return;
    }
    
    return '<div><strong>Associated Notes:</strong></div>';
    ?>
        // Obtain sphere nodes
        $query=new EntityFieldQuery();
        $query->entityCondition('entity_type','node')
              ->entityCondition('bundle','sphere');
        $sephiroths=$query->execute();
        if(array_key_exists('node',$sephiroths))
            $sephiroths=entity_load('node',array_keys($sephiroths['node']));
    
    
        // Obtain path nodes
        $query=new EntityFieldQuery();
        $query->entityCondition('entity_type','node')
              ->entityCondition('bundle','path');
        $paths=$query->execute();
        if(array_key_exists('node',$paths))
            $paths=entity_load('node',array_keys($paths['node']));
    
  • Similar by Taxonomy

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <ul class="item-list">
    <?php
    
    $terms_nood = taxonomy_node_get_terms_by_vocabulary($node,6);
    
    $terms = taxonomy_get_tree(6);
    
    foreach($terms as $term){
    
    	//$tagged = taxonomy_term_count_nodes($term->tid);
    
    	if( $term->name == arg(1) || array_key_exists( $term->tid,$terms_nood ) ){
    		echo("<li class=\"collapsed tag-category-".$popularityCategory."\">");
    		echo("<a href=\"/visual/".$term->name."\" class=\"active\">".$term->name."</a>");
    		echo("</li>\n");
    	}
    
    }
    
    ?>
    </ul>
    
    
    <?php
    if(arg(0)=='node'&&isset($node)){
        $vocabularies = taxonomy_get_vocabularies();
        foreach( $vocabularies as $vocabulary ){
            if( $vocabularies ){
                $terms = taxonomy_node_get_terms_by_vocabulary($node,$vocabulary->vid);
                if( $terms ){
                    echo('<div>');
                    $links = array();
                    echo('<h3>' . $vocabulary->name . '</h3>');
                    foreach( $terms as $term ){
                        $term_uri = taxonomy_term_uri($term);
                        $links[] = l($term->name,$term_uri['path'],array('rel'=>'tag','title'=>strip_tags($term->description)));
                    }
                    echo(implode(', ', $links));
                    echo('</div>');
                }
            }
        }
    }
    ?>
    
    
    <?php #if ($terms): ?>
    <?php
    $vocabularies = taxonomy_get_vocabularies();
    foreach( $vocabularies as $vocabulary ){
    	if( $vocabularies ){
    		$terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
    		if( $terms ){
    			echo("<div>\n");
    			$links = array();
    			echo( "<h3>" . $vocabulary->name . "</h3>\n" );
    			foreach( $terms as $term )
    				$links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
    			echo( implode(', ', $links) );
    			echo("</div>\n");
    		}
    	}
    }
    ?>
  • Rename stuck field names

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    
    $fields = array(
        'field_inspirational_imagery' => 'field_images',
    );
     
    // Loop through each of the fields/tables with the old name and change them
    foreach($fields as $field_name => $new_field_name) {
     
        // First check that field_name exists
        if(!db_table_exists('field_data_' . $field_name)) {
            // If we cannot find a data table then just continue.
            continue;
        }
     
        // Define some things...
        $data_table_name = 'field_data_' . $field_name;
        $revision_table_name = 'field_revision_' . $field_name;
        $field_info = field_info_field($field_name);
        $storage_details = $field_info['storage']['details'];
     
        // The storage for each field has unique configuration. Must follow.
        foreach($storage_details['sql']['FIELD_LOAD_CURRENT'] as $field) {
            // Change the field names.
            foreach($field as $key => $value) {
     
                // Rename the field table columns and preserve existing spec. Let
                // features take care of any configuration changes.
                $spec = $field_info['columns'][$key];
                db_change_field($data_table_name, $value, $new_field_name . "_" . $key, $spec);
                db_change_field($revision_table_name, $value, $new_field_name . "_" . $key, $spec);
     
            }
        }
     
        // Change the field storage table names.
        db_rename_table($data_table_name, 'field_data_' . $new_field_name);
        db_rename_table($revision_table_name, 'field_revision_' . $new_field_name);
     
        // Change the field names int he field_config and
        // field_instance_config tables
     
        db_update('field_config')
            ->fields(
                array(
                    'field_name' => $new_field_name,
                )
            )
            ->condition('field_name', $field_name, '=')
        ->execute();
     
        db_update('field_config_instance')
            ->fields(
                array(
                    'field_name' => $new_field_name,
                )
            )
            ->condition('field_name', $field_name, '=')
        ->execute();
    
    } /// end foreach loop on fields... whew.
    
    ?>
  • OSX Virtual Web Host Config

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics

    add line to /etc/hosts

    127.0.0.1 amniot.localhost

    add lines to /etc/apache2/httpd.conf

    NameVirtualHost 127.0.0.1:80
    
    <VirtualHost amniot.localhost:80>
    
    <Directory "/Library/Webserver/Documents_AMNIOT">
        Options FollowSymLinks Indexes
        AllowOverride All
        Order deny,allow
        allow from All
    </Directory>
    ServerName amniot.localhost
    DocumentRoot "/Library/Webserver/Documents_AMNIOT"
    
    </VirtualHost>
  • MySQL Basics

    Context: 
    Connection: 
    Attribute Type: 
    Types

    add export PATH=$PATH:/usr/local/mysql/bin to ~/.bash_profile or /etc/profile

    Backup database to text file

    mysqldump database_name > database_name.sql

    Remove database

    mysql> drop database database_name;

    Create database

    mysql> CREATE DATABASE database_name;
  • Manually reset Drupal admin password

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    45
    Attribute Type: 
    Topics
    <?php
    define('DRUPAL_ROOT', getcwd());
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    require_once DRUPAL_ROOT . '/includes/password.inc';
    if (isset($_GET['pass']) && !empty($_GET['pass'])) {
      $newhash =  user_hash_password($_GET['pass']);
    }
    else {
      die('Retry with ?pass=PASSWORD set in the URL');
    }
    $updatepass = db_update('users')
      ->fields(array(
        'pass' => $newhash,
    // Uncomment the following lines to reset the administrative username and/or email address, if necessary.
    //    'name' => 'admin',
    //    'mail' => 'yourmail@example.com'
      ))
      ->condition('uid', '1', '=')
      ->execute();
    print "Done. Please delete this file immediately!";
    drupal_exit();
    ?>
  • Nested-query approach to mining field_collection data and relationship

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    
    function get_species_country($id_country) {
        $inner = new EntityFieldQuery();
        $inner_r = $inner->entityCondition('entity_type', 'field_collection_item')
                         ->fieldCondition('field_species_country', 'target_id', $id_country, '=')
                         ->execute();
        if(!empty($inner_r['field_collection_item'])) {
            $keys = array_keys($inner_r['field_collection_item']);
            $query = new EntityFieldQuery();
            $species_r = $query->entityCondition('entity_type', 'node')
                          ->entityCondition('bundle', self::$bundle)
                          ->fieldCondition('field_species_countries', 'value', $keys)
                          ->execute();
            if(!empty($species_r['node'])) {
                $ret = node_load_multiple(array_keys($species_r['node']));
            }
        }
        return $ret;
    }
    
    ?>
  • Create symlinks

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    
    symlink('/homepages/21/d93015512/htdocs/library/organic_connectivity',
            '/homepages/21/d93015512/htdocs/drupal/sites/all/modules/organic_connectivity/lib');
    
    ?>
  • Trauma Informed Care

    Subtitle: 
    Information &amp; Blog
    Context: 
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Installation URL: 
    http://www.traumainformedhawaii.net
    Images: 
  • Various WP snippets

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics

    Show Links Menu w/Active Class

    <nav>
    <?php $args=array(
    	'sort_order' => 'ASC',
    	'sort_column' => 'post_date',
    	'hierarchical' => 1,
    	'exclude' => '',
    	'include' => '',
    	'meta_key' => '',
    	'meta_value' => '',
    	'authors' => '',
    	'child_of' => 0,
    	'parent' => -1,
    	'exclude_tree' => '',
    	'number' => '',
    	'offset' => 0,
    	'post_type' => 'page',
    	'post_status' => 'publish'
    );
    $pages = get_pages($args);
    $current_id = get_the_ID();
    if( $pages ){
        echo( "<ul>\n" );
        foreach( $pages as $page){
            $link_page_id = ($page->ID);
            if( $current_id==$link_page_id ) echo( "<li class=\"active\">" );
            else echo( "<li>" );
            echo( "<a href=\"".get_page_link($page->ID)."\">".$page->post_title."</a>" );
            echo( "</li>\n" );
        }
        echo( "</ul>\n\n" );
    }
    ?>
    </nav>

    Show Categories List

    <?php
        echo( "<div id=\"topics\">\n" );
        echo( "<h3>Topics</h3>\n" );
        echo( "<ul>\n" );
        $categories_obj = get_categories( array('orderby'=>'id','hide_empty'=>'1') );
        foreach( $categories_obj as $cat ){
            echo( "<li>" );
            echo( "<a href=\"/category/".$cat->slug."\">".$cat->name."</a>" );
            $posts_in_cat = new WP_Query( 'cat='.$cat->term_id );
            if ( $posts_in_cat->have_posts() ){
                echo( "<ul>\n" );
                while ( $posts_in_cat->have_posts() ){
                    $posts_in_cat->the_post();
                    echo( "<li><a href=\"");
                    the_permalink();
                    echo( "\">" );
                    the_title();
                    echo( "</a></li>\n" );
                }
                echo( "</ul>\n" );
            }
            echo( "</li>\n" );
        }
        echo( "</ul>\n" );
        echo( "</div>\n\n" );
    ?>

    Show Recent Posts List

    <?php
        echo( "<h2>What's New</h2>\n" );
        echo( "<ul>\n" );
        $latest_blog_posts = new WP_Query( array('posts_per_page'=>5) );
        if ( $latest_blog_posts->have_posts() ) :
            while ( $latest_blog_posts->have_posts() ) :
                $latest_blog_posts->the_post();
                // Loop output goes here
                echo( "<li><a href=\"");
                the_permalink();
                echo( "\">" );
                the_title();
                echo( "</a></li>\n" );
            endwhile;
        endif;
        echo( "</ul>\n" );
    }
    ?>

    Show Recent Posts Content

    <?php
        echo( "<?h2>Recent Posts<?/h2>\n" );
        $latest_blog_posts = new WP_Query( array( 'posts_per_page'=>5 ) );
        if ( $latest_blog_posts->have_posts() ){
            while ( $latest_blog_posts->have_posts() ){
                $latest_blog_posts->the_post();
                // Loop output goes here
                echo( "<?div class=\"entry\">\n" );
                echo( "<?h3>" );
                echo( "<?a href=\"" );
                the_permalink();
                echo( "\">" );
                the_title();
                echo( "<?/a>" );
                echo( "<?/h3>\n" );
                echo( "<?div class=\"time\">\n" );
                the_date();
                echo( "@" );
                the_time();
                echo( "<?/div>\n" );
                echo( "<?h4>in Categories<?/h4>" );
                the_category();
                the_content();
                echo( "<?/div>\n\n" );
            }
        }
    ?>

    Show Date Menu

    <?php
        echo("<h2>Archive</h2>\n");
        echo("<ul>\n");
        wp_get_archives();
        echo("</ul>\n");
    ?>

    Show Tag List

    <?php
        echo("<div id=\"tag-bar\">\n");
        echo("<ul>\n");
        $tags_obj = get_tags(array('orderby'=>'name','order'=>'ASC','hide_empty'=>'1'));
        foreach($tags_obj as $tag){
            echo("<li>");
            echo("<a href=\"/tag/".$tag->slug."\">".$tag->name."</a>");
            echo(" (".$tag->count.")");
            echo("</li>\n");
        }
        echo("</ul>\n");
        echo("</div>\n\n");
    ?>

    Add ability to execute php in text widgets

    <?php
    add_filter('widget_text','php_text',99);
    function php_text($text){
        if(strpos($text,'<'.'?')!==false){
            ob_start();
            eval('?'.'>'.$text);
            $text=ob_get_contents();
            ob_end_clean();
        }
        return $text;
    }
    ?>

    Removes the automatic addition of P and BR elements to content

    <?php
    remove_filter('the_content','wpautop');
    remove_filter('the_excerpt','wpautop');
    remove_filter('term_description','wpautop');
    ?>

    Using email as username in account registration

    <?php
    if(isset($_POST['user_login'])&&isset($_POST['user_email'])){
        $_POST['user_login']=$_POST['user_email'];
    }
    ?>

    Storing user-selected password into database on registration

    <?php
    add_action('user_register','ts_register_extra_fields',100);
    function ts_register_extra_fields($user_id){
        $userdata=array();
        $userdata['ID']=$user_id;
        if($_POST['user_password']!==''){
            $userdata['user_pass']=$_POST['user_password'];
        }
        $new_user_id=wp_update_user($userdata);
    }
    ?>

    Add ability to add featured images to pages and posts

    <?php
    add_theme_support('post-thumbnails');
    ?>

    Add ability to execute shortcodes in text widgets

    <?php
    add_filter('widget_text','do_shortcode');
    ?>

    Define menus

    <?php
    if(function_exists('register_nav_menus')){
        register_nav_menus(
            array(
                'main_menu'=>'Main Menu',
                'footer_menu'=>'Footer Menu',
            )
        );
    }
    ?>

    Define widget regions

    <?php
    if(function_exists('register_sidebar')){
        register_sidebar(array(
            'name'=>'Footer',
            'id'=>'footer',
            'before_widget'=>'<div>',
            'after_widget'=>'</div>',
            'before_title'=>'<h2>',
            'after_title'=>'</h2>',
        ));
        register_sidebar(array(
            'name'=>'Footer Right',
            'id'=>'footer_right',
            'before_widget'=>'<div>',
            'after_widget'=>'</div>',
            'before_title'=>'<h2>',
            'after_title'=>'</h2>',
        ));
        register_sidebar(array(
            'name'=>'Sidebar Right',
            'id'=>'sidebar_right',
            'before_widget'=>'<div>',
            'after_widget'=>'</div>',
            'before_title'=>'<h2>',
            'after_title'=>'</h2>',
        ));
    }
    ?>
  • Output a menu

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    
    
    $page_menu_items = menu_tree_output(menu_tree_all_data("main-menu"));
    
    echo( drupal_render($page_menu_items) );
    
    
    ?>
  • LDAP Connect and Bind Test

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    
    
    $host = "127.0.0.1:10389";
    $ldap = ldap_connect($host);
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
    
    
    $username = "uid=admin,ou=system";
    $password = "secret";
    
    
    if( $bind = ldap_bind($ldap, $username, $password) )
    
        echo( "logged into ".$host." as ".$username );
    
    else
    
        echo( "Fail" );
    
    
    ?>
  • Create custom content type

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics

    contenttypetest.info

    name = Contenttype test
    description = Test - how can I create a new content type with hook form?
    core = 7.x
    files[] = contenttypetest.module;
    files[] = contenttypetest.install;

    contenttypetest.module

    <?php
    
        // hook_form implementation
        function contenttypetest_form($node, &$form_state) {
            $form = array();
    
    
            $form['contenttypetest_pass'] = array(
                '#type' => 'password',
                '#title' => t('Type a password'),
                '#description' => t('You can type anything you like.'),
            );
            $form['contenttypetest_veld'] = array(
                '#type' => 'file',
                '#description' => 'You might wanna upload a file!',
                '#title' => 'Bestand',
            );
            return $form;
        }
    
    
        // hook_node_info() implementation
        function contenttypetest_node_info() {
            return array(
                'contenttypetest' => array(
                    'name' => t('Content type test node'),
                    'base' => 'contenttypetest',
                    'module' => 'contenttypetest',
                    'description' => t("This nodetype is a test how to create nodetypes."),
                    'help' => 'So this is how your new contenttype looks!',
                    'title_label' => t('Test'),
                    'has_body' => FALSE,
                ),
            );
        }
    
    
        // hook_validate() implementation
        function contenttypetest_validate(){
        }
    
    ?>

    contenttypetest.install

    <?php
    
        // hook_uninstall() implementation
        function contenttypetest_uninstall(){
            node_type_delete('contenttypetest');
        }
    
    ?>
  • Fill array with server request URI strings

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    
    $request = explode( "/", $_SERVER["REQUEST_URI"] );
    array_shift( $request );
    
    print_r( $requests );
    
    ?>
  • Render imagecache images

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    echo( "<img src=\"".image_style_url("thumbnail", $photo["uri"] )."\" alt=\"\"/>" );
  • Add unique IDs to menu items

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    <?php
    function HOOK_menu_link( array $variables ){
    
        $element = $variables['element'];
        $sub_menu = '';
        $name_id = strtolower(strip_tags($element['#title']));
    
        // remove colons and anything past colons
        if (strpos($name_id, ':')) $name_id = substr ($name_id, 0, strpos($name_id, ':'));
    
        //Preserve alphanumerics, everything else goes away
        $pattern = '/[^a-z]+/ ';
        $name_id = preg_replace($pattern, '', $name_id);
    
        $element['#attributes']['id'][] = 'menu-' . $element['#original_link']['mlid'] . ' '.$name_id;
    
        if( $element['#below'] )
            $sub_menu = drupal_render($element['#below']);
    
        $output = l($element['#title'], $element['#href'], $element['#localized_options']);
    
        return '' . $output . $sub_menu . "
  • \n"; } ?>
  • Obtain matching entities

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    function obtain_entities_by_type($entity_type,$bundle,$limit=10,$offset=0,$subject_nid=FALSE){
    
        $query=new EntityFieldQuery();
    
        $query->entityCondition('entity_type', $entity_type)
            ->entityCondition('bundle', $bundle)
            ->propertyCondition('status', 1)
            ->range($offset, $limit);
    
        if($subject_nid)
            $query->fieldCondition('field_subject','nid',$subject_nid);
    
        $results=$query->execute();
    
        if(array_key_exists('node',$results))
            return entity_load($entity_type,array_keys($results[$entity_type]));
    
    }

    Useage

    obtain_entities_by_type('node','page',10,0,432);

    Or

    <?php
    $sql="SELECT node.nid AS nid,
    node.title AS node_title,
    node.created AS node_created,
    node_data_field_weight.field_weight_value AS node_data_field_weight_field_weight_value
     FROM node node
     LEFT JOIN content_field_weight node_data_field_weight ON node.vid = node_data_field_weight.vid
     WHERE (node.status <> 0)
        ORDER BY node_data_field_weight_field_weight_value DESC";
    
    $db_query=db_query($sql);
    
    $items=array();
    while($row=db_fetch_array($db_query))
    	$items[]=array( $row["node_title"], $row["node_created"], $row["nid"], $row["node_data_field_weight_field_weight_value"] );
    
    echo("<pre>".print_r($items,TRUE)."</pre>\n\n");
    
    ?>

    Or

    <?php
    function snag_drupal_pages(){
        $sql_children="SELECT node.nid AS nid, node.vid AS node_vid, node.title AS node_title
         FROM node node
         WHERE node.type in ('page')";
        $db_query_children = db_query($sql_children);
        $pages=array();
        foreach($db_query_children as $row){
        	$nd=node_load($row->nid);
        	$key=array_push( $pages,array() ) - 1;
        	$pages[$key]["id"] = $row->nid;
        	$pages[$key]["title"] = $nd->title;
        	$pages[$key]["date"] = $nd->created;
        	if( drupal_lookup_path('alias',"node/".$nd->nid) ) $pages[$key]["path"] = drupal_lookup_path('alias',"node/".$nd->nid);
        	else $pages[$key]["path"] = "node/".$nd->nid;
        	if( !empty($nd->field_parent_page) ) $pages[$key]["group"] = $nd->field_parent_page["und"][0]["nid"];
        }
        #echo("<pre>");print_r($pages);echo("</pre>\n");
        return $pages;
    }
    ?>
  • Show field when a different selection is made on the same form

    Context: 
    Connection: 
    Attribute Type: 
    Types
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    Connection: 
    Weight: 
    100
    Attribute Type: 
    Topics
    function HOOK_form_alter( &$form, &$form_state, $form_id ){
    
        if( $form_id == "user_register_form" || $form_id == "user_profile_form" ) {
    
    
            // Shows OTHER text field when 'Other' is selected as their specialty
    
            $form["field_specialty_other"]["#states"] = array(
                "visible" => array(
                    ':input[name="field_specialty[und]"]' => array("value"=>"Other"),
                )
            );
    
    
        }
    
    }