ANNOUNCEMENT - Mogdesign joins i-KOS Digital - click to read why...

2/3 Using custom fields in the feed/csv

28
May
2014

This is part 2 of Aegir mini-series.

Using feed to install multiple sites at once is a cool feature, but when you start using it, you will quickly realize that you need customizations for your sites. Of course, you will use your own shiny installation profile which you made from lots of coffee*, but how do you personalize every specific site with some special parameters (e.g. site country)? What, hard-coded mapping in the profile? Really? The customer just called, he wants to add 50 new sites... Since you are using feeds already, why not put the specific data into it? Well, it’s not a trivial task for starters - but I am here to guide you through the dark path.

The following custom module for Aegir needs to be created:

hosting_country.install:

/**
* Implementation of hook_schema().
*/
function hosting_country_schema() {
  $schema['site_country'] = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'country' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'vid' => array('vid'),
      'country' => array('country'),
    ),
  );
  return $schema;
}
 
/**
 * Implementation of hook_install().
 */
function hosting_country_install() {
  // Create tables.
  drupal_install_schema('hosting_country');
}
 
/**
 * Implementation of hook_uninstall().
 */
function hosting_country_uninstall() {
  // Remove tables.
  drupal_uninstall_schema('hosting_country');
}

hosting_country.module:

/**
 * Implementation of hook_form_alter()
 */
function hosting_country_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'site_node_form') {
    $countries = _country_get_predefined_list();
 
    $form['site_country'] = array(
      '#type' => 'select',
      '#title' => t('Site country'),
      '#empty_value' => '',
      '#options' => $countries,
      '#description' => t('Select the site country.'),
      '#weight' => 10,
    );
 
    if (!is_null($form['nid']['#value'])) {
      $node = node_load($form['nid']['#value']);
      $form['site_country']['#default_value'] = $node->site_country;
      $form['site_country']['#disabled'] = TRUE;
    }
 
    return $form;
  }
}
 
/**
 * Implementation of hook_insert()
 */
function hosting_country_insert($node) {
  if ($node->{'site_country'}) {
    db_query("INSERT INTO {site_country} (vid, nid, country) VALUES (%d, %d, '%s')", $node->vid, $node->nid, $node->{'site_country'});
  }
}
/**
 * Implementation of hook_delete()
 */
function hosting_country_delete($node) {
  db_query("DELETE FROM {site_country} WHERE 'nid' = %d", $node->nid);
}
/**
 * Implementation of hook_delete_revision()
 */
function hosting_country_delete_revision($node) {
  db_query("DELETE FROM {site_country} WHERE 'vid' = %d", $node->vid);
}
 
/**
 * Implementation of hook_nodeapi()
 */
function hosting_country_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->type == 'site') {
    switch ($op) {
    case 'insert':
        hosting_country_insert($node);
        break;
      case 'delete' :
        hosting_country_delete($node);
        break;
      case 'delete revision':
        hosting_country_delete_revision($node);
        break;
      case 'validate' :
        if (!$node->{'site_country'}) {
          form_set_error('site_country', t('You must choose a country!'));
        }
        break;
      case 'load':
        $additions['site_country'] = db_result(db_query("SELECT country FROM {site_country} WHERE vid = %d", $node->vid));
        return $additions;
        break;
    }
  }
}

hosting_country.drush.inc:

/**
* Implementation of drush_hosting_site_pre_hosting_task()
* Send the site's attributes to the backend for processing.
*/
function drush_hosting_country_pre_hosting_task() {
  $task =& drush_get_context('HOSTING_TASK');
  if ($task->ref->type == 'site' && ($task->task_type == 'install' || $task->task_type == 'verify')) {
    $task->options['site_country'] = $task->ref->{'site_country'};
  }
}

Feeds integration (either in module file or separate submodule):

 t('Site Country'),
      'description' => t('The site country to use for the site.'),
      'callback' => 'hosting_country_feeds_set_target_site_country',
    );
  }
}
 
/**
 * Feeds mapper callback for Aegir platforms.
 */
function hosting_country_feeds_set_target_site_country($node, $target, $value) {
  $node->$target = $value;
}

Use the variable in installation profile:

drush_get_option('site_country');

 

*Programmer – definition: an organic machine which converts caffeine into source code

 

About the author

m1n0's picture

Milan is a web developer with more than 5 years of experience in web development and Drupal. Milan is a technology enthusiast and he loves to play with new gadgets and development techniques. He is also a musician and he loves to play drums with his band.

LIKED THE POST? SPREAD THE WORD!

del.icio.usStumbleUponDiggFacebookLinkedIn

6 comments so far

Oliver's picture

Great blog :)

faw3232's picture

According to the Association of infertile couples in Quebec, 5,000 children were born since the public program assisted reproduction. "If it was not the program, these children would not be here today," said Krystel Lavigne, Vice-President of the Association

fifa 15 ultimate team coins

Oklahoma's picture

Thanks for well-composed Aegir mini-series. Eager to read more of it.

creativewriting's picture

very good

*Programmer – definition: an organic machine which converts caffeine into source code

saiful's picture

WoW !
Is it a building. I con't belive. But nice.

dghff3ygdfgh54@inbox.com's picture

You may be competent to do as numerous extra soccer celebrities, as well as Messi. Execute in the AI. Take advantage of bonuses that are numerous, the same as snowfall in addition to slime. Enhance the statistics. Because you may do numerous techniques as well as position while puppet soccer champions hack pour android ios telecharger aspires which are a great deal.

Add new comment