You are here

class TitleResolver in Easy Breadcrumb 8

Same name and namespace in other branches
  1. 2.x src/TitleResolver.php \Drupal\easy_breadcrumb\TitleResolver

Class TitleResolver.

Hierarchy

Expanded class hierarchy of TitleResolver

1 string reference to 'TitleResolver'
easy_breadcrumb.services.yml in ./easy_breadcrumb.services.yml
easy_breadcrumb.services.yml
1 service uses TitleResolver
easy_breadcrumb.title_resolver in ./easy_breadcrumb.services.yml
Drupal\easy_breadcrumb\TitleResolver

File

src/TitleResolver.php, line 17

Namespace

Drupal\easy_breadcrumb
View source
class TitleResolver extends ControllerTitleResolver {

  /**
   * The field storage config storage.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Breadcrumb config object.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Constructs a new EntityDisplayRebuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
    $this->entityTypeManager = $entity_type_manager;
    $this->config = $config_factory
      ->get(EasyBreadcrumbConstants::MODULE_SETTINGS);
  }

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

  /**
   * {@inheritdoc}
   */
  public function getTitle(Request $request, Route $route) {
    $url = Url::fromUri("internal:" . $request
      ->getRequestUri());
    $route_parts = explode(".", $url
      ->getRouteName());
    $entity = NULL;
    $params = $url
      ->getRouteParameters();
    if ($route_parts[0] === 'entity' && $route_parts[2] === 'canonical') {
      $entity_type = $route_parts[1];
      $entity = $this->entityTypeManager
        ->getStorage($entity_type)
        ->load($params[$entity_type]);
    }
    if ($entity !== NULL) {
      $alternative_title_field = $this->config
        ->get(EasyBreadcrumbConstants::ALTERNATIVE_TITLE_FIELD);
      if ($entity
        ->hasField($alternative_title_field) && !$entity
        ->get($alternative_title_field)
        ->isEmpty()) {
        return Xss::filter($entity
          ->get($alternative_title_field)->value);
      }
    }
    return parent::getTitle($request, $route);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
TitleResolver::$argumentResolver protected property The argument resolver.
TitleResolver::$config protected property Breadcrumb config object.
TitleResolver::$controllerResolver protected property The controller resolver.
TitleResolver::$entityTypeManager protected property The field storage config storage.
TitleResolver::create public static function
TitleResolver::getTitle public function Returns a static or dynamic title for the route. Overrides TitleResolver::getTitle
TitleResolver::__construct public function Constructs a new EntityDisplayRebuilder. Overrides TitleResolver::__construct