- Issue created by @mariacha1
- πΊπΈUnited States mariacha1
This isn't a robust-enough solution to be a patch, but here's the ImportProcessor I'm using in my custom module if it's useful for other people:
<?php declare(strict_types=1); namespace Drupal\mymodule\Plugin\EntityShareClient\Processor; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginFormInterface; use Drupal\entity_share_client\ImportProcessor\ImportProcessorPluginBase; use Drupal\entity_share_client\RuntimeImportContext; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Import book structure. * * @todo This can be deprecated if * https://www.drupal.org/project/entity_share/issues/3441672 is fixed. * * @ImportProcessor( * id = "metatag_importer", * label = @Translation("Use Server Canonical link"), * description = @Translation("Get the Canonical url from the Metatag attribute on the Server."), * stages = { * "prepare_importable_entity_data" = 20, * }, * locked = false, * ) */ class MetatagImporter extends ImportProcessorPluginBase implements PluginFormInterface { /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityFieldManagerInterface */ protected $entityFieldManager; /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); $instance->entityFieldManager = $container->get('entity_field.manager'); return $instance; } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { return $form; } /** * {@inheritdoc} */ public function prepareImportableEntityData(RuntimeImportContext $runtime_import_context, array &$entity_json_data): void { if (isset($entity_json_data['attributes']['metatag'])) { // Find out if there's a Metatag type field on the node. $parsed_type = explode('--', $entity_json_data['type']); $entity_type_id = $parsed_type[0]; $entity_bundle = $parsed_type[1]; $entity_fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $entity_bundle); $metatag_field = NULL; foreach ($entity_fields as $id => $field) { if ($field->getType() === 'metatag') { $metatag_field = $id; } } if ($metatag_field && $entity_json_data['attributes'][$metatag_field] === NULL) { foreach ($entity_json_data['attributes']['metatag'] as $value) { if (isset($value['attributes']['rel']) && $value['attributes']['rel'] === 'canonical') { $entity_json_data['attributes'][$metatag_field] = [ 'value' => [ 'canonical_url' => $value['attributes']['href'], ], ]; } } } } } }
This would be enabled as a plugin called "Use Server Canonical link" at the import config level under `/admin/config/services/entity_share/import_config`.