You are here

public function BreadcrumbList::getItems in Schema.org Metatag 8.2

Overrides ItemListElement::getItems

File

src/Plugin/schema_metatag/PropertyType/BreadcrumbList.php, line 68

Class

BreadcrumbList
Provides a plugin for the 'ItemListElement' Schema.org property type.

Namespace

Drupal\schema_metatag\Plugin\schema_metatag\PropertyType

Code

public function getItems($input_value) {
  $values = [];
  if (!empty($input_value)) {
    $entity_route = \Drupal::service('current_route_match')
      ->getCurrentRouteMatch();
    $breadcrumbs = \Drupal::service('breadcrumb')
      ->build($entity_route)
      ->getLinks();
    $key = 1;
    foreach ($breadcrumbs as $item) {

      // Modules that add the current page to the breadcrumb set it to an
      // empty path, so an empty path is the current path.
      $url = $item
        ->getUrl()
        ->setAbsolute()
        ->toString();
      if (empty($url)) {
        $url = Url::fromRoute('<current>')
          ->setAbsolute()
          ->toString();
      }
      $text = $item
        ->getText();
      $text = is_object($text) ? $text
        ->render() : $text;
      $values[$key] = [
        '@id' => $url,
        'name' => $text,
        'item' => $url,
      ];
      $key++;
    }
  }
  return $values;
}