MetatagQuery.php in GatherContent 8.5
File
src/MetatagQuery.php
View source
<?php
namespace Drupal\gathercontent;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MetatagQuery implements ContainerInjectionInterface {
protected $entityFieldManager;
protected $configFactory;
public function __construct(EntityFieldManagerInterface $entityFieldManager, ConfigFactoryInterface $configFactory) {
$this->entityFieldManager = $entityFieldManager;
$this->configFactory = $configFactory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_field.manager'), $container
->get('config.factory'));
}
public function checkMetatag($entityType, $contentType) {
$instances = $this->entityFieldManager
->getFieldDefinitions($entityType, $contentType);
foreach ($instances as $instance) {
if ($instance
->getType() === 'metatag') {
return TRUE;
}
}
return FALSE;
}
public function getFirstMetatagField($entityType, $contentType) {
$instances = $this->entityFieldManager
->getFieldDefinitions($entityType, $contentType);
foreach ($instances as $instance) {
if ($instance
->getType() === 'metatag') {
return $instance
->getName();
}
}
return '';
}
public function getMetatagFields($entityType, $contentType) {
$metatagManager = \Drupal::service('metatag.manager');
$settings = $this->configFactory
->get('metatag.settings');
$entityTypeGroups = $settings
->get('entity_type_groups');
$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;
}
}