User Tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
drupal [2021/11/02 13:07] – created ssm2017drupal [2022/02/07 13:29] (current) – external edit 127.0.0.1
Line 4: Line 4:
 <sxh> <sxh>
 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 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
 +</sxh>
 +
 +=== simple drupal 7 field ===
 +== external_image_link.info ==
 +<sxh ini>
 +name = External image field
 +description = "Display an image link as an image"
 +package = Fields
 +core = 7.x
 +files[] = external_image_field.module
 +</sxh>
 +
 +== external_image_link.install ==
 +<sxh php>
 +/**
 + * 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
 +  );
 +}</sxh>
 +
 +== external_image_link.module ==
 +<sxh php>
 +/**
 + * 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;
 +  }
 +}
 +
 +</sxh>
 +
 +=== Batch by drush ===
 +== Sources ==
 +  * http://www.metaltoad.com/blog/using-drupal-batch-api
 +  * http://victorquinn.com/blog/2012/08/11/programmatically-creating-fielded-d7-nodes/
 +== 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 :
 +  * title
 +  * introtext
 +  * fulltext
 +  * state
 +  * created
 +  * created_by
 +  * modified
 +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 ==
 +<sxh bash>
 +~/.drush/bash_import.drush.inc
 +</sxh>
 +<sxh php>
 +<?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);
 +}
 +
 </sxh> </sxh>
  

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information