Document how to migrate to ECK objects

Created on 8 May 2024, 6 months ago
Updated 23 May 2024, 6 months ago

Build a source plugin for FPP entities.

πŸ“Œ Task
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @DamienMcKenna
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Seems to work.

    
    namespace Drupal\htportal_migrate\Plugin\migrate\source;
    
    use Drupal\migrate\Row;
    use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
    
    // cspell:ignore fpid
    
    /**
     * Drupal 7 FPP source from database.
     *
     * Available configuration keys:
     * - fpp_type: The FPP types to get from the source - can be a string or an
     *   array. If not declared then FPPs of all types will be retrieved.
     *
     * Examples:
     *
     * @code
     * source:
     *   plugin: d7_fpp_entity
     *   fpp_type: thing
     * @endcode
     *
     * In this example FPPs of type "thing" are retrieved from the source database.
     *
     * @code
     * source:
     *   plugin: d7_fpp_entity
     *   fpp_type: [thing, test]
     * @endcode
     *
     * In this example FPPs of type page and test are retrieved from the source
     * database.
     *
     * For additional configuration keys, refer to the parent classes.
     *
     * @see \Drupal\migrate\Plugin\migrate\source\SqlBase
     * @see \Drupal\migrate\Plugin\migrate\source\SourcePluginBase
     *
     * @MigrateSource(
     *   id = "d7_fpp_entity",
     *   source_module = "fieldable_panels_panes"
     * )
     */
    class Fpp extends FieldableEntity {
    
      /**
       * The join options between the FPP and the FPP revisions table.
       */
      const JOIN = 'fpp.vid = fppr.vid';
    
      /**
       * {@inheritdoc}
       */
      public function query() {
        // Select FPP in its last revision.
        $query = $this->select('fieldable_panels_panes_revision', 'fppr')
          ->fields('fpp', [
            'fpid',
            'vid',
            'bundle',
            'title',
            'link',
            'path',
            'reusable',
            'admin_title',
            'admin_description',
            'category',
            'view_access',
            'edit_access',
            'language',
            'created',
            'changed',
            'uuid',
          ])
          ->fields('fppr', [
            'fpid',
            'vid',
            'timestamp',
            'uid',
            'title',
            'log',
            'vuuid',
          ]);
        $query->innerJoin('fieldable_panels_panes', 'fpp', static::JOIN);
    
        if (isset($this->configuration['fpp_type'])) {
          $query->condition('fpp.bundle', (array) $this->configuration['fpp_type'], 'IN');
        }
    
        return $query;
      }
    
      /**
       * {@inheritdoc}
       */
      public function prepareRow(Row $row) {
        $fpid = $row->getSourceProperty('fpid');
        $vid = $row->getSourceProperty('vid');
        $type = $row->getSourceProperty('bundle');
    
        // If this entity was translated using Entity Translation, we need to get
        // its source language to get the field values in the right language.
        $entity_translatable = $this->isEntityTranslatable('fieldable_panels_pane') && (int) $this->variableGet('language_content_type_' . $type, 0) === 4;
        $source_language = $this->getEntityTranslationSourceLanguage('fieldabpe_panels_pane', $fpid);
        $language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
    
        // Get Field API field values.
        foreach ($this->getFields('fieldable_panels_pane', $type) as $field_name => $field) {
          // Ensure we're using the right language if the entity and the field are
          // translatable.
          $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
          $row->setSourceProperty($field_name, $this->getFieldValues('fieldable_panels_pane', $field_name, $fpid, $vid, $field_language));
        }
    
        return parent::prepareRow($row);
      }
    
      /**
       * {@inheritdoc}
       */
      public function fields() {
        $fields = [
          // From the main FPP table.
          'fpid' => $this->t('FPP ID'),
          'vid' => $this->t('Revision ID'),
          'bundle' => $this->t('Bundle'),
          'title' => $this->t('Title'),
          'link' => $this->t('Link'),
          'path' => $this->t('Path'),
          'reusable' => $this->t('Reusable'),
          'admin_title' => $this->t('Admin title'),
          'admin_description' => $this->t('Admin description'),
          'category' => $this->t('Category'),
          'view_access' => $this->t('View access'),
          'edit_access' => $this->t('Edit access'),
          'language' => $this->t('Language (fr, en, ...)'),
          'created' => $this->t('Created timestamp'),
          'changed' => $this->t('Modified timestamp'),
          'uuid' => $this->t('UUID'),
          // From the FPP revisions table.
          'timestamp' => $this->t('The timestamp the latest revision of this FPP was created.'),
          'uid' => $this->t('FPP authored by (uid)'),
          'log' => $this->t('The revision log.'),
        ];
        return $fields;
      }
    
      /**
       * {@inheritdoc}
       */
      public function getIds() {
        $ids['fpid']['type'] = 'integer';
        $ids['fpid']['alias'] = 'fpp';
        return $ids;
      }
    
    }
    

    Not sure about some of the translation stuff, but it works for language-free data.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    I migrated the FPP data to the "custom_data" ECK data structure with this:

    langcode: en
    status: true
    dependencies: {  }
    id: upgrade_d7_organizations
    field_plugin_method: null
    cck_plugin_method: null
    migration_tags:
      - 'Drupal 7'
      - Content
    migration_group: mysite
    label: 'Organizations'
    source:
      plugin: d7_fpp_entity
    process:
      type:
        -
          plugin: default_value
          default_value: organization
      id: fpid
      title:
        -
          source: title
          plugin: str_replace
          search: '''
          replace: "'"
        -
          plugin: default_value
          default_value: 'Unnamed organization'
      uid: uid
      created: created
      changed: timestamp
      field_site_id:
        -
          source: field_siteid/0/value
          plugin: skip_on_empty
          method: row
          message: "No site ID provided."
    destination:
      plugin: 'entity:custom_data'
      translations: false
      default_bundle: organization
    migration_dependencies:
      required:
        - upgrade_d7_user
      optional: {}
    
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    It's so long since I looked at the codebase I forgot it already had source plugins.

    This is some documentation that explains how to modify the migration to create ECK objects.

  • Status changed to Needs review 6 months ago
  • Status changed to Fixed 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Committed.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024