You are here

protected function EntityShareMetatagEnhancer::doUndoTransform in Entity Share 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/jsonapi/FieldEnhancer/EntityShareMetatagEnhancer.php \Drupal\entity_share\Plugin\jsonapi\FieldEnhancer\EntityShareMetatagEnhancer::doUndoTransform()

File

src/Plugin/jsonapi/FieldEnhancer/EntityShareMetatagEnhancer.php, line 72

Class

EntityShareMetatagEnhancer
Prepare metatag value to be able to shared.

Namespace

Drupal\entity_share\Plugin\jsonapi\FieldEnhancer

Code

protected function doUndoTransform($data, Context $context) {

  // Export.
  $configuration = $this
    ->getConfiguration();
  if ($configuration['expose_default_tags']) {

    // Inspired from MetatagManager::generateRawElements() and from
    // https://www.drupal.org/project/metatag/issues/2945817#comment-13079626.

    /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
    $field_item = $context['field_item_object'];
    $entity = $field_item
      ->getEntity();
    $metatags_for_entity = $this->metatagManager
      ->tagsFromEntityWithDefaults($entity);
    if ($configuration['replace_tokens']) {
      $token_replacements = [
        $entity
          ->getEntityTypeId() => $entity,
      ];
      $replacements_options = [];
      if ($configuration['clear_tokens']) {
        $replacements_options['clear'] = TRUE;
      }
      $data = [];
      foreach ($metatags_for_entity as $metatag_key => $metatag_for_entity) {
        $data[$metatag_key] = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->token
          ->replace($metatag_for_entity, $token_replacements, $replacements_options)));
        if (empty($data[$metatag_key])) {
          unset($data[$metatag_key]);
        }
      }
    }
    else {
      $data = $metatags_for_entity;
    }
  }
  return [
    'value' => $data,
  ];
}