You are here

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

1 call to ItemListElement::getItems()
ItemListElement::outputValue in src/Plugin/schema_metatag/PropertyType/ItemListElement.php
Transform input value to its display output.
1 method overrides ItemListElement::getItems()
BreadcrumbList::getItems in src/Plugin/schema_metatag/PropertyType/BreadcrumbList.php

File

src/Plugin/schema_metatag/PropertyType/ItemListElement.php, line 91

Class

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

Namespace

Drupal\schema_metatag\Plugin\schema_metatag\PropertyType

Code

public function getItems($input_value) {

  // A simple array of values.
  $list = $this
    ->schemaMetatagManager()
    ->explode($input_value);
  if (is_array($list)) {
    return $list;
  }
  elseif (strpos(':', $input_value) !== FALSE) {
    return $input_value;
  }
  else {
    $values = [];
    $args = explode(':', $input_value);
    if (empty($args)) {
      return $values;
    }

    // Load the requested view.
    $view_id = array_shift($args);
    $view = Views::getView($view_id);

    // Set the display.
    if (count($args) > 0) {
      $display_id = array_shift($args);
      $view
        ->setDisplay($display_id);
    }
    else {
      $view
        ->initDisplay();
    }

    // See if the page's arguments should be passed to the view.
    if (count($args) == 1 && $args[0] == '{{args}}') {
      $view_path = explode("/", $view
        ->getPath());
      $current_url = Url::fromRoute('<current>');
      $query_args = explode("/", substr($current_url
        ->toString(), 1));
      $args = [];
      foreach ($query_args as $index => $arg) {
        if (in_array($arg, $view_path)) {
          unset($query_args[$index]);
        }
      }
      if (!empty($query_args)) {
        $args = array_values($query_args);
      }
    }

    // Allow modules to alter the arguments passed to the view.
    \Drupal::moduleHandler()
      ->alter('schema_item_list_views_args', $args);
    if (!empty($args)) {
      $view
        ->setArguments($args);
    }
    $view
      ->preExecute();
    $view
      ->execute();

    // Get the view results.
    $key = 1;
    foreach ($view->result as $item) {

      // If this is a display that does not provide an entity in the result,
      // there is really nothing more to do.
      $entity = static::getEntityFromRow($item);
      if (!$entity) {
        return '';
      }

      // Get the absolute path to this entity.
      $url = $entity
        ->toUrl()
        ->setAbsolute()
        ->toString();
      $values[$key] = [
        '@id' => $url,
        'name' => $entity
          ->label(),
        'url' => $url,
      ];
      $key++;
    }
  }
  return $values;
}