1

Drupal 8.8
Modules: Metatags & Migration plus.

I am trying to convert a tag in an XML file to an open graph image metatag.

File: modules/custom/my_module/dataxml.html
This is the url that has to be converted to the image metatag.

<ImageUrl>
  <![CDATA[https://via.placeholder.com/300.jpg/09f/fff]]>
</ImageUrl>

File: modules/custom/my_module/config/install/migrate_plus.migration.content.yml
These are the source field for the required metatag.

source:
  fields:
     - name: og_image_url
       label: Image
       selector: ImageUrl

Process for the required metatag

process:
  field_meta_tags:
    source: og_image_url
    plugin: prepare_meta

File: modules/custom/my_module/src/Plugin/migrate/process/PrepareMeta.php
The plugin links the metatag with the required value and serializes the mapped result

<?php

namespace Drupal\my_module\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
 * Plugin to prepare the meta tags.
 *
 * @MigrateProcessPlugin(
 *   id = "prepare_meta",
 * )
 */
class PrepareMeta extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $metas = [];

    // This should likely work with an array later on, but for now, it'll do.
    if (!empty($value)) {
      $metas[$this->configuration['source']] = $value;
    }

    return $metas ? serialize($metas) : [];
  }
}

When I run xDebug $metas has the following value:
$metas = [ og_image_url => "https://via.placeholder.com/300.jpg/09f/fff" ];

The node is created and the other field are migrated perfectly. However the metatag field og:image:url remains empty and doesn't trigger an error. I don't really know what I'm doing wrong here. If someone could point me in the right direction, that would be much appreciated.

1 Answer 1

0

Turns out I used the wrong field name. My field is called field_meta_tag instead of field_meta_tags as used in the question above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.