Table of Contents

Drupal

d7

d7 base modules

drush dl module_filter admin_menu views ctools pathauto sitemap bootstrap jquery_update date devel metatag xmlsitemap libraries transliteration token field_group autocomplete_deluxe entity rules calendar date_ical

simple drupal 7 field

external_image_link.info

name = External image field
description = "Display an image link as an image"
package = Fields
core = 7.x
files[] = external_image_field.module

external_image_link.install

/**
 * Implements hook_field_schema().
 */
function external_image_field_field_schema($field) {
  $columns = array(
    'title' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
    'thumb_url' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
    'full_url' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
  );
  return array(
    'columns' => $columns
  );
}

external_image_link.module

/**
 * Implements hook_field_info().
 */
function external_image_field_field_info() {
  return array(
    'external_image_field_external_image' => array(
      'label' => t('External image link'),
      'description' => t('Display a link to an image.'),
      'default_widget' => 'external_image_field_text',
      'default_formatter' => 'external_image_field_simple_text',
    ),
  );
}

/**
 * Implements hook_field_is_empty().
 */
function external_image_field_field_is_empty($item, $field) {
  return empty($item['full_url']);
}

/**
 * Implements hook_field_formatter_info().
 */
function external_image_field_field_formatter_info() {
  return array(
    'external_image_field_simple_text' => array(
      'label' => t('Simple text-based formatter'),
      'field types' => array('external_image_field_external_image'),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function external_image_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  foreach ($items as $delta => $item) {
    $variables = array(
      'path' => $item['thumb_url'],
      'alt' => 'Test alt',
      'title' => 'Test title',
      'attributes' => array(),
    );
    $element[$delta] = array(
      '#markup' => l(theme_image($variables), $item['full_url'], array('html' => TRUE, 'attributes' => array('title' => $item['title'], 'class' => 'colorbox'))),
    );
  }
  return $element;
}

/**
 * Implements hook_field_widget_info().
 */
function external_image_field_field_widget_info() {
  return array(
    'external_image_field_text' => array(
      'label' => t('Default'),
      'field types' => array('external_image_field_external_image'),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function external_image_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $element['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Image title'),
    '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : '',
  );
  $element['thumb_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Image thumb URL'),
    '#default_value' => isset($items[$delta]['thumb_url']) ? $items[$delta]['thumb_url'] : '',
  );
  $element['full_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Image full URL'),
    '#default_value' => isset($items[$delta]['full_url']) ? $items[$delta]['full_url'] : '',
  );
  return $element;
}

/**
 * Implements hook_field_widget_error().
 */
function external_image_field_field_widget_error($element, $error, $form, &$form_state) {
  switch ($error['error']) {
    case 'external_image_field_invalid':
      form_error($element, $error['message']);
      break;
  }
}

Batch by drush

Sources
How ?

I needed to import some content from a Joomla! website. I have exported the content using phpmyadmin and asking to get the data as a php array from the table 'jos_content' and get only the fields named :

Then, i have used a drush script to import them. It tooks about 30 seconds for 280 nodes. The exported file was 1.2MB

Code

~/.drush/bash_import.drush.inc
<?php

function drush_import_drush_command() {
  $items  = array();
  $items['drush_import'] = array(
    'callback'    =&gt; 'drush_import_setup_batch',
    'description' =&gt; dt('Import some nodes'),
    'arguments'   =&gt; array(
      'start'     =&gt; "start",
      'stop'      =&gt; "stop",
    ),
  );
  return $items;
}
 
function drush_import_drush_help($section) {
  switch ($section) {
    case 'drush:drush_import':
      return dt("blablabla.");
  }
}

function drush_import_setup_batch($start=1, $stop=100000) {
  //  ...
  //  Populate $lots_of_data from record $start to record $stop.
  //  ...
 include '/path/jos_content.php';

  //Break up all of our data so each process does not time out.
  $chunks = array_chunk($jos_content, 20);
  $operations = array();
  $count_chunks = count($chunks);
 
  //for every chunk, assign some method to run on that chunk of data
  foreach ($chunks as $chunk) {
    $i++;
    $operations[] = array(
      "drush_import_some_nodes",
      array(
        $chunk ,
        'details'=&gt; t('(Importing chunk @chunk  of  @count)', array('@chunk '=&gt;$i, '@count'=&gt;$count_chunks))
      )
    );
  }
 
  //put all that information into our batch array
  $batch = array(
    'operations' =&gt; $operations,
    'title' =&gt; t('Import batch'),
    'init_message' =&gt; t('Initializing'),
    'error_message' =&gt; t('An error occurred'),
    'finished' =&gt; 'drush_import_finished_method'
  );
 
  //Get the batch process all ready!
  batch_set($batch);
  $batch =&amp; batch_get();
 
  //Because we are doing this on the back-end, we set progressive to false.
  $batch['progressive'] = FALSE;
 
  //Start processing the batch operations.
  drush_backend_batch_process();
}

function drush_import_some_nodes($chunk, $operation_details, &amp;$context) {
  print("chunk size : ". count($chunk). "\n");
  foreach ($chunk as $content) {
    print("importing : ". $content['title']. "\n");
    drush_import_create_node($content);
  }
  $context['message'] = $operation_details; //Will show what chunk we're on.
}

function drush_import_finished_method($success, $results, $operations) {
  //Let the user know we have finished!
  print t("Finished importing!\n");
}

function drush_import_create_node($content) {
  // title
  // introtext
  // fulltext
  // state
  // created
  // created_by
  // modified
  $node = new stdClass();
  $node-&gt;type = 'blog';
  node_object_prepare($node);
  $node-&gt;uid = '1';
  $node-&gt;name = 'admin';
  $node-&gt;language = LANGUAGE_NONE;
  $node-&gt;title = $content['title'];
  $node-&gt;status = $content['state'];
  $node-&gt;created = strtotime($content['created']);
  $node-&gt;changed = strtotime($content['modified']);
  $node-&gt;body[LANGUAGE_NONE][0]['summary'] = $content['introtext'];
  $node-&gt;body[LANGUAGE_NONE][0]['value'] = $content['fulltext'];
  $node-&gt;body[LANGUAGE_NONE][0]['format'] = 'full_html';
  node_save($node);
}

function mymodule_menu() {
  $items['mymodule/%'] = array(
    'title' => 'My module callback',
    'type' => MENU_CALLBACK,
    'page callback' => 'mymodule_callback',
    'page arguments' => array(1),
    'access callback' => TRUE,
  );
}

function mymodule_node_view($node, $build_mode = 'full') {
  if ($node->type == 'mynodetype' && $build_mode == 'full') {
      $node->content['mylink'] = array(
        '#type' => 'link',
        '#title' => t('Do the magic'),
        '#href' => 'mymodule/'.$node->nid,
        '#prefix' => '<div id="mylink">',
        '#suffix' => '</div><div id="mylink-ajax-result"></div>',
        '#ajax' => array(
          'effect' => 'fade',
        ),
      );
  }
}

function mymodule_callback($nid) {
  $output = 'Yeah';
  $commands = array();
  $commands[] = ajax_command_replace('#mylink-ajax-result', $output);
  $page = array('#type' => 'ajax', '#commands' => $commands);
  ajax_deliver($page);
}

d7 quick vertical tabs

$form['main_fieldset'] = array(
  '#type' => 'fieldset',
  '#title' => 'my fieldset'
);
$form['main_fieldset']['mytabs'] = array(
  '#type' => 'vertical_tabs',
);
$form['main_fieldset']['other'] = array(
  '#type' => 'fieldset',
  '#title' => 'my fieldset',
  '#group' => 'mytabs'
);

d7 relations

Here are some pieces of code to play with nodes and “relation” module.

function relation_new_child_node_view($node, $build_mode = 'full') {
  if ($build_mode == 'full') {
    // get available relations
    $relation_types = relation_get_available_types('node', $node-&gt;type, 'target');
    if ($relation_types) {
      $content = array();
      foreach ($relation_types as $relation_type) {
        // get available node types
        $nodes_types = array();
        $items = array();
        foreach ($relation_type-&gt;source_bundles as $source_bundle) {
          $values = explode(':', $source_bundle);
          $nodes_types[$values[1]] = node_type_get_name($values[1]);
          $items[] = l(
            t('Create a new !type', array('!type' =&gt; $nodes_types[$values[1]])),
            'node/add/' . $values[1],
            array(
              'query' =&gt; array(
              'destination' =&gt; 'node/' . $node-&gt;nid,
                'rt' =&gt; $relation_type-&gt;relation_type,
                'nid' =&gt; $node-&gt;nid
              )
            )
          );
        }
        if (!empty($items)) {
          $content['relation_links'] = array(
            '#markup' =&gt; theme_item_list(array(
              'items' =&gt; $items,
              'title' =&gt; t('Create related content'),
              'type' =&gt; 'ul',
              'attributes' =&gt; array()
              ))
          );
        }
        // get related content
        $query = relation_query('node', $node-&gt;nid);
        $query-&gt;entityCondition('bundle', $relation_type-&gt;relation_type);
        $results = $query-&gt;execute();
        if ($results) {
          // get the related nodes
          $related_nodes = array();
          foreach ($results as $result) {
            $relation = relation_load($result-&gt;rid);
            $entities = field_get_items('relation', $relation, 'endpoints');
            $related_nodes[] = node_load((int)$entities[0]['entity_id']);
          }
          if (!empty($related_nodes)) {
            $nodes_types = array_unique($nodes_types);
            foreach ($nodes_types as $node_type =&gt; $node_type_name) {
              $items = array();
              foreach ($related_nodes as $related_node) {
                if ($related_node-&gt;type == $node_type) {
                  $items[] = l($related_node-&gt;title, 'node/' . $related_node-&gt;nid, array('attributes' =&gt; array('title' =&gt; $related_node-&gt;title)));
                }
              }
              if (!empty($items)) {
                $content['related_links'][$node_type] = array(
                  '#markup' =&gt; theme_item_list(array(
                    'items' =&gt; $items,
                    'title' =&gt; $node_type_name,
                    'type' =&gt; 'ul',
                    'attributes' =&gt; array()
                    ))
                );
              }
            }
          }
        }
      }
      if (!empty($content['related_links'])) {
        $node-&gt;content[] = $content['related_links'];
      }
      if (!empty($content['relation_links'])) {
        $node-&gt;content[] = $content['relation_links'];
      }
    }
  }
}

function relation_new_child_node_insert($node) {
  dsm('passed');
  if ((isset($_GET['rt']) &amp;&amp; !empty($_GET['rt'])) &amp;&amp; (isset($_GET['nid']) &amp;&amp; !empty($_GET['nid']))) {
    $endpoints = array();
    $endpoints[] = array('entity_type' =&gt; 'node', 'entity_id' =&gt; $node-&gt;nid);
    $endpoints[] = array('entity_type' =&gt; 'node', 'entity_id' =&gt; $_GET['nid']);

    $new_relation = relation_create($_GET['rt'], $endpoints);

    if ($rid = relation_save($new_relation)) {
      drupal_set_message(t('Relation created'), 'status', FALSE);
    }
    else {
      drupal_set_message(t('Creating relation failed'), 'warning', FALSE);
    }
  }
}

d8

d8 install

sudo apt install php-gd
sudo apt install php-mbstring
composer create-project drupal/recommended-project:8.x d8test