View source
<?php
namespace Drupal\metatag\Plugin\migrate\source\d7;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\MigrationDeriverTrait;
use Drupal\migrate_drupal\MigrationConfigurationTrait;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MetatagFieldInstanceDeriver extends DeriverBase implements ContainerDeriverInterface {
use MigrationDeriverTrait;
use MigrationConfigurationTrait;
use StringTranslationTrait;
protected $supportedEntityTypesTables = [
'node' => [
'node',
'node_type',
],
'taxonomy_term' => [
'taxonomy_term_data',
'taxonomy_vocabulary',
],
'user' => [],
];
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('entity_type.manager'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$source = $this
->getSourcePlugin($base_plugin_definition['source']['plugin']);
assert($source instanceof DrupalSqlBase);
try {
$source
->checkRequirements();
} catch (RequirementsException $e) {
return $this->derivatives;
}
foreach ($this->supportedEntityTypesTables as $entity_type_id => $entity_type_tables) {
if (!$this->entityTypeManager
->hasDefinition($entity_type_id)) {
continue;
}
foreach ($entity_type_tables as $entity_type_table) {
if (!$source
->getDatabase()
->schema()
->tableExists($entity_type_table)) {
continue;
}
}
$base_query = $source
->getDatabase()
->select('metatag', 'm');
$base_query
->condition('m.entity_type', $entity_type_id);
$metatag_count = (int) (clone $base_query)
->countQuery()
->execute()
->fetchField();
if ($metatag_count === 0) {
continue;
}
$metatags_grouped_by_bundle = [];
switch ($entity_type_id) {
case 'node':
$base_query
->join('node', 'n', "n.nid = m.entity_id");
$base_query
->fields('n', [
'type',
]);
$base_query
->join('node_type', 'nt', 'nt.type = n.type');
$base_query
->fields('nt', [
'name',
]);
$base_query
->groupBy('n.type');
$base_query
->groupBy('nt.name');
$rows = $base_query
->execute()
->fetchAllAssoc('type');
$metatags_grouped_by_bundle = array_reduce($rows, function (array $carry, $row) {
$carry[$row->type] = $row->name;
return $carry;
}, []);
break;
case 'taxonomy_term':
$base_query
->join('taxonomy_term_data', 'ttd', "ttd.tid = m.entity_id");
$base_query
->fields('ttd', [
'vid',
]);
$base_query
->join('taxonomy_vocabulary', 'tv', 'ttd.vid = tv.vid');
$base_query
->fields('tv', [
'machine_name',
'name',
]);
$base_query
->groupBy('ttd.vid');
$base_query
->groupBy('tv.machine_name');
$base_query
->groupBy('tv.name');
$rows = $base_query
->execute()
->fetchAllAssoc('machine_name');
$metatags_grouped_by_bundle = array_reduce($rows, function (array $carry, $row) {
$carry[$row->machine_name] = $row->name;
return $carry;
}, []);
break;
}
if (!empty($metatags_grouped_by_bundle)) {
foreach ($metatags_grouped_by_bundle as $bundle_id => $bundle_label) {
$derivative_id = "{$entity_type_id}:{$bundle_id}";
$this->derivatives[$derivative_id] = $base_plugin_definition;
$this->derivatives[$derivative_id]['source']['entity_type_id'] = $entity_type_id;
$this->derivatives[$derivative_id]['source']['entity_type'] = $entity_type_id;
$this->derivatives[$derivative_id]['source']['bundle'] = $bundle_id;
$this->derivatives[$derivative_id]['label'] = $this
->t('@label of @type @entity-type-label', [
'@label' => $base_plugin_definition['label'],
'@type' => $bundle_label,
'@entity-type-label' => $this->entityTypeManager
->getDefinition($entity_type_id)
->getPluralLabel(),
]);
foreach ([
'd7_metatag_field_instance',
] as $dep_id) {
$dependency_index = array_search($dep_id, $this->derivatives[$derivative_id]['migration_dependencies']['required']);
if ($dependency_index !== FALSE) {
$this->derivatives[$derivative_id]['migration_dependencies']['required'][$dependency_index] .= ":{$entity_type_id}:{$bundle_id}";
}
}
switch ($entity_type_id) {
case 'node':
$this->derivatives[$derivative_id]['migration_dependencies']['required'][] = "d7_node_type:{$bundle_id}";
break;
case 'taxonomy_term':
$this->derivatives[$derivative_id]['migration_dependencies']['required'][] = "d7_taxonomy_vocabulary:{$bundle_id}";
break;
}
foreach ([
'd7_metatag_field',
] as $dep_id) {
$dependency_index = array_search($dep_id, $this->derivatives[$derivative_id]['migration_dependencies']['required']);
if ($dependency_index !== FALSE) {
$this->derivatives[$derivative_id]['migration_dependencies']['required'][$dependency_index] .= ":{$entity_type_id}";
}
}
}
}
else {
$this->derivatives[$entity_type_id] = $base_plugin_definition;
$this->derivatives[$entity_type_id]['source']['entity_type_id'] = $entity_type_id;
$this->derivatives[$entity_type_id]['label'] = $this
->t('@label of @type', [
'@label' => $base_plugin_definition['label'],
'@type' => $this->entityTypeManager
->getDefinition($entity_type_id)
->getPluralLabel(),
]);
foreach ([
'd7_metatag_field',
'd7_metatag_field_instance',
] as $dep_id) {
$dependency_index = array_search($dep_id, $this->derivatives[$entity_type_id]['migration_dependencies']['required']);
if ($dependency_index !== FALSE) {
$this->derivatives[$entity_type_id]['migration_dependencies']['required'][$dependency_index] .= ":{$entity_type_id}";
}
}
}
}
return $this->derivatives;
}
}