You are here

class SeoManager in Real-time SEO for Drupal 8.2

Utility service for the Real-Time SEO module.

@package Drupal\yoast_seo

Hierarchy

Expanded class hierarchy of SeoManager

1 file declares its use of SeoManager
YoastSeoWidget.php in src/Plugin/Field/FieldWidget/YoastSeoWidget.php
1 string reference to 'SeoManager'
yoast_seo.services.yml in ./yoast_seo.services.yml
yoast_seo.services.yml
1 service uses SeoManager
yoast_seo.manager in ./yoast_seo.services.yml
Drupal\yoast_seo\SeoManager

File

src/SeoManager.php, line 17

Namespace

Drupal\yoast_seo
View source
class SeoManager {
  use StringTranslationTrait;

  /**
   * Entity Type Manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Entity Type Bundle Info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * Entity Field Manager service.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Constructor for YoastSeoManager.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity Type Manager service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
   *   Entity Type Bundle Info service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   Entity Field Manager service.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $stringTranslation
   *   The string traslation service.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo, EntityFieldManagerInterface $entityFieldManager, TranslationInterface $stringTranslation) {
    $this->entityTypeBundleInfo = $entityTypeBundleInfo;
    $this->entityTypeManager = $entityTypeManager;
    $this->entityFieldManager = $entityFieldManager;
    $this->stringTranslation = $stringTranslation;
  }

  /**
   * Returns an array of bundles that have a 'yoast_seo' field.
   *
   * @return array
   *   A nested array of entities and bundles. The outer array is keyed by
   *   entity id. The inner array is keyed by bundle id and contains the bundle
   *   label. If an entity has no bundles then the inner array is keyed by
   *   entity id.
   */
  public function getEnabledBundles() {
    $entities = [];

    /** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
    foreach ($this->entityTypeManager
      ->getDefinitions() as $definition) {
      $entity_id = $definition
        ->id();
      $bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($entity_id);
      foreach ($bundles as $bundle_id => $bundle_metadata) {
        $bundle_label = $bundle_metadata['label'];
        $field_definitions = $this->entityFieldManager
          ->getFieldDefinitions($entity_id, $bundle_id);
        if (!empty($field_definitions['yoast_seo'])) {
          if (!isset($entities[$entity_id])) {
            $entities[$entity_id] = [];
          }
          $entities[$entity_id][$bundle_id] = $bundle_label;
        }
      }
    }
    return $entities;
  }

  /**
   * Returns the Real-Time SEO field of the entity.
   *
   * Returns the first field of the entity that is a Real-Time SEO field or
   * null if none is found.
   *
   * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
   *   The entity for which to find the Real-Time SEO field.
   *
   * @return null|\Drupal\Core\Field\FieldItemListInterface
   *   The field item list of the field or NULL if no RTSEO field was found.
   */
  public function getSeoField(FieldableEntityInterface $entity) {
    $definitions = $entity
      ->getFieldDefinitions();

    // Find the first yoast_seo field on the entity.
    foreach ($definitions as $definition) {
      if ($definition
        ->getType() === 'yoast_seo') {
        return $entity
          ->get($definition
          ->getName());
      }
    }

    // No field of yoast_seo type was found.
    return NULL;
  }

  /**
   * Get the status for a given score.
   *
   * @todo Move this back to something like an SEO Assessor.
   *
   * @param int $score
   *   Score in points.
   *
   * @return string
   *   Status corresponding to the score.
   */
  public function getScoreStatus($score) {
    $rules = $this
      ->getScoreRules();
    foreach ($rules as $minimum => $label) {

      // As soon as our score is bigger than a rules threshold, use that label.
      if ($score >= $minimum) {
        return $label;
      }
    }
    return $this
      ->t('Unknown');
  }

  /**
   * Retrieves the score rules from configuration.
   *
   * @return string[]
   *   A list of labels indexed by the minimum score required. Ordered from high
   *   to low.
   */
  public function getScoreRules() {
    $rules = \Drupal::config('yoast_seo.settings')
      ->get('score_rules');

    // Ensure rules are sorted from high to low score.
    ksort($rules);
    return array_reverse($rules, TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SeoManager::$entityFieldManager protected property Entity Field Manager service.
SeoManager::$entityTypeBundleInfo protected property Entity Type Bundle Info service.
SeoManager::$entityTypeManager protected property Entity Type Manager service.
SeoManager::getEnabledBundles public function Returns an array of bundles that have a 'yoast_seo' field.
SeoManager::getScoreRules public function Retrieves the score rules from configuration.
SeoManager::getScoreStatus public function Get the status for a given score.
SeoManager::getSeoField public function Returns the Real-Time SEO field of the entity.
SeoManager::__construct public function Constructor for YoastSeoManager.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.