FieldCollectionType.php 3.14 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\paragraphs\Plugin\migrate\source\d7;

use Drupal\migrate\Row;
6
use Drupal\paragraphs\Plugin\migrate\field\FieldCollection;
7
use Drupal\paragraphs\Plugin\migrate\source\DrupalSqlBase;
8 9

/**
10 11 12 13 14
 * Field Collection Type source plugin.
 *
 * Available configuration keys:
 * - add_description: (bool) (optional) If enabled this will add a default
 *   description to the source data. default:FALSE.
15 16 17 18 19 20 21 22
 *
 * @MigrateSource(
 *   id = "d7_field_collection_type",
 *   source_module = "field_collection"
 * )
 */
class FieldCollectionType extends DrupalSqlBase {

23 24 25 26 27 28 29 30 31
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'add_description' => FALSE,
    ] + parent::defaultConfiguration();
  }

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('field_config', 'fc')
      ->fields('fc');
    $query->condition('fc.type', 'field_collection');
    $query->condition('fc.active', TRUE);
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {

    $name = $row->getSourceProperty('field_name');

50
    // Remove field_ prefix for new bundle.
51
    $bundle = substr($name, FieldCollection::FIELD_COLLECTION_PREFIX_LENGTH);
52 53 54 55
    $row->setSourceProperty('bundle', $bundle);

    // Field collections don't have descriptions, optionally add one.
    if ($this->configuration['add_description']) {
56 57 58 59 60 61 62 63
      $row->setSourceProperty('description', 'Migrated from field_collection ' . $name);
    }
    else {
      $row->setSourceProperty('description', '');
    }

    // Set label from bundle because we don't have a label in D7 field
    // collections.
64
    $row->setSourceProperty('name', ucfirst(preg_replace('/_/', ' ', $bundle)));
65 66 67 68 69 70 71 72 73

    return parent::prepareRow($row);
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [
74
      'field_name' => $this->t('Original field collection bundle/field_name'),
75 76 77 78 79 80 81 82 83 84
      'bundle' => $this->t('Paragraph type machine name'),
      'name' => $this->t('Paragraph type label'),
      'description' => $this->t('Paragraph type description'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
85 86
    $ids['field_name']['type'] = 'string';

87 88 89
    return $ids;
  }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  /**
   * {@inheritdoc}
   *
   * @deprecated addDescription configuration key is deprecated in 8.x-1.0 and
   * will be removed prior to 9.x.  Use add_description instead.
   *
   * @see https://www.drupal.org/project/paragraphs/issues/2911242
   */
  public function setConfiguration(array $configuration) {

    // Configuration keys should be snake_case, not CamelCase. Adding a BC
    // layer for addDescription which should be deprecated.
    if (isset($configuration['addDescription'])) {
      @trigger_error("addDescription configuration key is deprecated in 8.x-1.0 and will be removed prior to 9.x.  Use add_description instead. See https://www.drupal.org/project/paragraphs/issues/2911242", E_USER_DEPRECATED);
      if (!isset($configuration['add_description'])) {
        $configuration['add_description'] = $configuration['addDescription'];
      }
      unset($configuration['addDescription']);
    }

    parent::setConfiguration($configuration);
  }

113
}