You are here

public function PathautoPattern::prepareRow in Pathauto 8

Adds additional data to the row.

Parameters

\Drupal\migrate\Row $row: The row object.

Return value

bool FALSE if this row needs to be skipped.

Overrides SourcePluginBase::prepareRow

File

src/Plugin/migrate/source/PathautoPattern.php, line 85

Class

PathautoPattern
Fetches pathauto patterns from the source database.

Namespace

Drupal\pathauto\Plugin\migrate\source

Code

public function prepareRow(Row $row) {
  $entity_definitions = $this->entityTypeManager
    ->getDefinitions();
  $name = $row
    ->getSourceProperty('name');

  // Pattern variables are made of pathauto_[entity type]_[bundle]_pattern.
  // First let's find a matching entity type from the variable name.
  foreach ($entity_definitions as $entity_type => $definition) {

    // Check if this is the default pattern for this entity type.
    // Otherwise, check if this is a pattern for a specific bundle.
    if ($name == 'pathauto_' . $entity_type . '_pattern') {

      // Set process values.
      $row
        ->setSourceProperty('id', $entity_type);
      $row
        ->setSourceProperty('label', (string) $definition
        ->getLabel() . ' - default');
      $row
        ->setSourceProperty('type', 'canonical_entities:' . $entity_type);
      $row
        ->setSourceProperty('pattern', unserialize($row
        ->getSourceProperty('value')));
      return parent::prepareRow($row);
    }
    elseif (strpos($name, 'pathauto_' . $entity_type . '_') === 0) {

      // Extract the bundle out of the variable name.
      preg_match('/^pathauto_' . $entity_type . '_([a-zA-z0-9_]+)_pattern$/', $name, $matches);
      $bundle = $matches[1];

      // Check that the bundle exists.
      $bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($entity_type);
      if (!in_array($bundle, array_keys($bundles))) {

        // No matching bundle found in destination.
        return FALSE;
      }

      // Set process values.
      $row
        ->setSourceProperty('id', $entity_type . '_' . $bundle);
      $row
        ->setSourceProperty('label', (string) $definition
        ->getLabel() . ' - ' . $bundles[$bundle]['label']);
      $row
        ->setSourceProperty('type', 'canonical_entities:' . $entity_type);
      $row
        ->setSourceProperty('pattern', unserialize($row
        ->getSourceProperty('value')));
      $selection_criteria = [
        'id' => $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type,
        'bundles' => [
          $bundle => $bundle,
        ],
        'negate' => FALSE,
        'context_mapping' => [
          $entity_type => $entity_type,
        ],
      ];
      $row
        ->setSourceProperty('selection_criteria', [
        $selection_criteria,
      ]);
      return parent::prepareRow($row);
    }
  }
  return FALSE;
}