You are here

abstract class MigrateSimpleFieldHandler in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/destinations/fields.inc \MigrateSimpleFieldHandler

Base class for creating field handlers for fields with a single value.

To use this class just extend it and pass key where the field's value should be stored to the constructor, then register the type(s):

class MigrateLinkFieldHandler extends MigrateSimpleFieldHandler {
  public function __construct() {
    parent::__construct('url');
    $this
      ->registerTypes(array(
      'link',
    ));
  }

}

Hierarchy

Expanded class hierarchy of MigrateSimpleFieldHandler

File

plugins/destinations/fields.inc, line 252
Support for processing entity fields

View source
abstract class MigrateSimpleFieldHandler extends MigrateFieldHandler {
  protected $fieldValueKey = 'value';
  protected $skipEmpty = FALSE;

  /**
   * Construct a simple field handler.
   *
   * @param $options
   *   Array of options (rather than unamed parameters so you don't have to
   *   what TRUE or FALSE means). The following keys are used:
   *   - 'value_key' string with the name of the key in the fields value array.
   *   - 'skip_empty' Boolean indicating that empty values should not be saved.
   */
  public function __construct($options = array()) {
    if (isset($options['value_key'])) {
      $this->fieldValueKey = $options['value_key'];
    }
    if (isset($options['skip_empty'])) {
      $this->skipEmpty = $options['skip_empty'];
    }
  }
  public function prepare($entity, array $field_info, array $instance, array $values) {
    $arguments = array();
    if (isset($values['arguments'])) {
      $arguments = $values['arguments'];
      unset($values['arguments']);
    }
    $language = $this
      ->getFieldLanguage($entity, $field_info, $arguments);

    // Let the derived class skip empty values.
    if ($this->skipEmpty) {
      $values = array_filter($values, array(
        $this,
        'notNull',
      ));
    }

    // Setup the Field API array for saving.
    $delta = 0;
    foreach ($values as $value) {
      if (is_array($language)) {
        $current_language = $language[$delta];
      }
      else {
        $current_language = $language;
      }
      $return[$current_language][] = array(
        $this->fieldValueKey => $value,
      );
      $delta++;
    }
    return isset($return) ? $return : NULL;
  }

  /**
   * Returns TRUE only for values which are not NULL.
   *
   * @param $value
   *
   * @return bool
   */
  protected function notNull($value) {
    return !is_null($value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateFieldHandler::getFieldLanguage function Determine the language of the field.
MigrateHandler::$dependencies protected property List of other handler classes which should be invoked before the current one.
MigrateHandler::$typesHandled protected property List of "types" handled by this handler. Depending on the kind of handler, these may be destination types, field types, etc.
MigrateHandler::getDependencies public function
MigrateHandler::getTypesHandled public function
MigrateHandler::handlesType public function Does this handler handle the given type? 1
MigrateHandler::registerTypes protected function Register a list of types handled by this class
MigrateSimpleFieldHandler::$fieldValueKey protected property
MigrateSimpleFieldHandler::$skipEmpty protected property
MigrateSimpleFieldHandler::notNull protected function Returns TRUE only for values which are not NULL. 2
MigrateSimpleFieldHandler::prepare public function
MigrateSimpleFieldHandler::__construct public function Construct a simple field handler. Overrides MigrateHandler::__construct 3