You are here

class ParagraphsMigrateParagraphsFieldHandler in Paragraphs 7

Provides migrate field handler for paragraphs field.

Hierarchy

Expanded class hierarchy of ParagraphsMigrateParagraphsFieldHandler

1 string reference to 'ParagraphsMigrateParagraphsFieldHandler'
paragraphs_migrate_api in ./paragraphs.migrate.inc
Implements hook_migrate_api().

File

migrate/fields/ParagraphsMigrateParagraphsFieldHandler.inc, line 6

View source
class ParagraphsMigrateParagraphsFieldHandler extends MigrateSimpleFieldHandler {

  /**
   * {@inheritdoc}
   */
  public function __construct() {
    parent::__construct(array(
      'value_key' => 'value',
      'skip_empty' => TRUE,
    ));
    $this
      ->registerTypes(array(
      'paragraphs',
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function fields($type, $instance, $migration = NULL) {
    return array(
      'revision_id' => t('Option: Provide optional revision id.'),
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function notNull($value) {
    return !is_null($value) && $value !== FALSE;
  }

  /**
   * {@inheritdoc}
   */
  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',
      ));
    }

    // Do not proceed if we got no values.
    if (empty($values)) {
      return NULL;
    }
    $revision_ids = $this
      ->getRevisionIds($values, $arguments);

    // 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,
        'revision_id' => $revision_ids[$delta],
      );
      $delta++;
    }
    return isset($return) ? $return : NULL;
  }

  /**
   * Helper to get set of revision ids for import.
   */
  protected function getRevisionIds(array $values, array $arguments) {
    $return = array();
    if (!isset($arguments['revision_id'])) {
      $arguments['revision_id'] = array();
    }
    elseif (!is_array($arguments['revision_id'])) {
      $arguments['revision_id'] = array(
        $arguments['revision_id'],
      );
    }
    $revision_ids = db_select('paragraphs_item', 'p')
      ->fields('p', array(
      'item_id',
      'revision_id',
    ))
      ->condition('item_id', $values)
      ->execute()
      ->fetchAllKeyed();
    foreach ($values as $delta => $item_id) {

      // Get revision ID provided by the migration.
      if (!empty($arguments['revision_id'][$delta])) {
        $return[$delta] = $arguments['revision_id'][$delta];
      }
      else {
        $return[$delta] = $revision_ids[$item_id];
      }
    }
    return $return;
  }

}

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
ParagraphsMigrateParagraphsFieldHandler::fields public function
ParagraphsMigrateParagraphsFieldHandler::getRevisionIds protected function Helper to get set of revision ids for import.
ParagraphsMigrateParagraphsFieldHandler::notNull protected function Returns TRUE only for values which are not NULL. Overrides MigrateSimpleFieldHandler::notNull
ParagraphsMigrateParagraphsFieldHandler::prepare public function Overrides MigrateSimpleFieldHandler::prepare
ParagraphsMigrateParagraphsFieldHandler::__construct public function Construct a simple field handler. Overrides MigrateSimpleFieldHandler::__construct