You are here

class MetatagQuery in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 src/MetatagQuery.php \Drupal\gathercontent\MetatagQuery

Class for querying metatag data.

Hierarchy

Expanded class hierarchy of MetatagQuery

2 files declare their use of MetatagQuery
ContentProcessor.php in src/Import/ContentProcess/ContentProcessor.php
GcImportTestBase.php in tests/src/Kernel/GcImportTestBase.php
1 string reference to 'MetatagQuery'
gathercontent.services.yml in ./gathercontent.services.yml
gathercontent.services.yml
1 service uses MetatagQuery
gathercontent.metatag in ./gathercontent.services.yml
Drupal\gathercontent\MetatagQuery

File

src/MetatagQuery.php, line 12

Namespace

Drupal\gathercontent
View source
class MetatagQuery implements ContainerInjectionInterface {
  protected $entityFieldManager;

  /**
   * MetatagQuery constructor.
   */
  public function __construct(EntityFieldManagerInterface $entityFieldManager) {
    $this->entityFieldManager = $entityFieldManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_field.manager'));
  }

  /**
   * Check if content type has any metatag fields.
   *
   * @param string $content_type
   *   Machine name of content type.
   *
   * @return bool
   *   TRUE if metatag field exists.
   */
  public function checkMetatag($content_type) {
    $instances = $this->entityFieldManager
      ->getFieldDefinitions('node', $content_type);
    foreach ($instances as $name => $instance) {

      /** @var \Drupal\Core\Field\FieldDefinitionInterface $instance */
      if ($instance
        ->getType() === 'metatag') {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Get list of metatag fields.
   *
   * @param string $content_type
   *   Machine name of content type.
   *
   * @return array
   *   Array of metatag fields.
   */
  public function getMetatagFields($content_type) {
    $instances = $this->entityFieldManager
      ->getFieldDefinitions('node', $content_type);
    $fields = [];
    foreach ($instances as $name => $instance) {

      /** @var \Drupal\Core\Field\FieldDefinitionInterface $instance */
      if ($instance
        ->getType() === 'metatag') {
        $fields[] = $instance
          ->getName();
      }
    }
    return $fields;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MetatagQuery::$entityFieldManager protected property
MetatagQuery::checkMetatag public function Check if content type has any metatag fields.
MetatagQuery::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
MetatagQuery::getMetatagFields public function Get list of metatag fields.
MetatagQuery::__construct public function MetatagQuery constructor.