You are here

public function AmpMergeMetadata::getMergedMetadata in Accelerated Mobile Pages (AMP) 8

Helper function to get merged global and per node type metadata.

Parameters

string $node_type: The node type.

Return value

array An array containing merged metadata for this node type.

File

src/Utility/AmpMergeMetadata.php, line 67

Class

AmpMergeMetadata
Class AmpMergeMetadata

Namespace

Drupal\amp\Utility

Code

public function getMergedMetadata($node_type) {

  /** @var AmpMetadataInterface $metadata_entity */
  foreach ($this->metadataEntities as $metadata_entity) {

    // Check if these settings are for global or type override matching the node
    // type. Because the if selector uses OR, store this outside if statement.
    $is_global = $is_type = FALSE;
    $is_global = $metadata_entity
      ->isGlobal();
    $is_type = $metadata_entity
      ->getNodeType() === $node_type;
    if ($is_global || $is_type) {

      // Get metadata settings from config entity.
      $entity_settings = [];

      // Get organization metadata.
      if (!empty($org_name = $metadata_entity
        ->getOrganizationName())) {
        $entity_settings['org_name'] = $org_name;
      }
      if (!empty($org_logo_fid = $metadata_entity
        ->getOrganizationLogoFid())) {
        $entity_settings['org_logo_fid'] = $org_logo_fid;
      }
      if (!empty($org_logo_style_id = $metadata_entity
        ->getOrganizationLogoImageStyleId())) {
        $entity_settings['org_logo_style_id'] = $org_logo_style_id;
      }

      // Get content metadata.
      if (!empty($schema_type = $metadata_entity
        ->getContentSchemaType())) {
        $entity_settings['schema_type'] = $schema_type;
      }
      if (!empty($content_headline_token = $metadata_entity
        ->getContentHeadlineToken())) {
        $entity_settings['content_headline_token'] = $content_headline_token;
      }
      if (!empty($content_author_token = $metadata_entity
        ->getContentAuthorToken())) {
        $entity_settings['content_author_token'] = $content_author_token;
      }
      if (!empty($content_description_token = $metadata_entity
        ->getContentDescriptionToken())) {
        $entity_settings['content_description_token'] = $content_description_token;
      }
      if (!empty($content_image_token = $metadata_entity
        ->getContentImageToken())) {
        $entity_settings['content_image_token'] = $content_image_token;
      }
      if (!empty($content_image_style_id = $metadata_entity
        ->getContentImageStyleId())) {
        $entity_settings['content_image_style_id'] = $content_image_style_id;
      }

      // Store entity settings as global or per type so overrides can be merged.
      if ($is_global) {
        $this->ampGlobalMetadata = $entity_settings;
      }
      elseif ($is_type) {
        $this->ampNodeTypeMetadata = $entity_settings;
      }
    }
  }
  return array_merge($this->ampGlobalMetadata, $this->ampNodeTypeMetadata);
}