You are here

public function MetatagQuery::getMetatagFields in GatherContent 8.5

Same name and namespace in other branches
  1. 8.4 src/MetatagQuery.php \Drupal\gathercontent\MetatagQuery::getMetatagFields()

Get list of metatag fields.

Parameters

string $entityType: Machine name of the entity type.

string $contentType: Machine name of content type.

Return value

array Array of metatag fields.

File

src/MetatagQuery.php, line 109

Class

MetatagQuery
Class for querying metatag data.

Namespace

Drupal\gathercontent

Code

public function getMetatagFields($entityType, $contentType) {

  // Use Drupal service instead of dependency injection.
  // Because we don't want the module to be dependent on metatag module.
  $metatagManager = \Drupal::service('metatag.manager');

  // Retrieve configuration settings.
  $settings = $this->configFactory
    ->get('metatag.settings');
  $entityTypeGroups = $settings
    ->get('entity_type_groups');

  // See if there are requested groups for this entity type and bundle.
  $filteredGroups = !empty($entityTypeGroups[$entityType]) && !empty($entityTypeGroups[$entityType][$contentType]) ? $entityTypeGroups[$entityType][$contentType] : [];
  $groups = $metatagManager
    ->sortedGroupsWithTags();
  $fields = [];
  foreach ($groups as $key => $group) {
    if (empty($group['tags']) || !empty($filteredGroups) && !in_array($key, $filteredGroups)) {
      continue;
    }
    $groupName = $group['label'];
    $fields[$groupName] = [];
    foreach ($group['tags'] as $tag) {
      $fields[$groupName][$tag['id']] = $tag['label'];
    }
  }
  return $fields;
}