You are here

public function MetatagsFieldProcessor::extractTranslatableData in Translation Management Tool 8

Extracts the translatatable data structure from the given field.

Parameters

\Drupal\Core\Field\FieldItemListInterface $field: The field object.

Return value

array $data An array of elements where each element has the following keys:

  • #text
  • #translate

Overrides DefaultFieldProcessor::extractTranslatableData

See also

\Drupal\tmgmt_content\Plugin\tmgmt\Source\ContentEntitySource::extractTranslatableData()

File

sources/content/src/MetatagsFieldProcessor.php, line 16

Class

MetatagsFieldProcessor
Field processor for the metatags field.

Namespace

Drupal\tmgmt_content

Code

public function extractTranslatableData(FieldItemListInterface $field) {
  $metatag_manager = \Drupal::service('metatag.manager');
  $meta_tag_values = unserialize($field->value);

  // If there are no meta tags or it is not an array, there is nothing to
  // do.
  if (empty($meta_tag_values) || !is_array($meta_tag_values)) {
    return [];
  }

  // Get the groups and tags information with their labels.
  $groups_and_tags = $metatag_manager
    ->sortedGroupsWithTags();

  // @todo Use the schema instead of hardcoding this list once
  //   https://www.drupal.org/node/2907214 is fixed.
  $blacklist_tags = [
    'robots',
    'referrer',
    'twitter_cards_type',
  ];
  $data = [];

  // Group the tags
  foreach ($groups_and_tags as $group_name => $group) {
    if (!isset($group['tags'])) {
      continue;
    }
    $tags = array_intersect_key($group['tags'], $meta_tag_values);
    if ($tags) {
      $data[$group_name] = [
        '#label' => $group['label'],
      ];
      foreach ($tags as $tag_name => $tag) {
        $data[$group_name][$tag_name] = [
          '#translate' => !in_array($tag_name, $blacklist_tags),
          '#text' => $meta_tag_values[$tag_name],
          '#label' => $tag['label'],
        ];
      }
    }
  }
  if (!empty($data)) {
    $data['#label'] = $field
      ->getFieldDefinition()
      ->getLabel();
  }
  return $data;
}