You are here

public function SimpleSitemapViews::addArgumentsToIndex in Simple XML sitemap (Views integration) 8

Adds view arguments to the index.

Parameters

\Drupal\views\ViewExecutable $view: A view executable instance.

array $args: Array of arguments to add to the index.

string|null $display_id: The display id. If empty uses the preselected display.

Return value

bool TRUE if the arguments are added to the index, FALSE otherwise.

File

src/SimpleSitemapViews.php, line 111
Contains simple_sitemap_views service.

Class

SimpleSitemapViews
Class to manage sitemap data for views.

Namespace

Drupal\simple_sitemap_views

Code

public function addArgumentsToIndex(ViewExecutable $view, array $args, $display_id = NULL) {

  // An array of arguments to be added to the index can not be empty.
  // Also ensure the display was correctly set.
  if (empty($args) || !$view
    ->setDisplay($display_id)) {
    return FALSE;
  }

  // Check that indexing of at least one argument is enabled.
  $indexable_arguments = $this
    ->getIndexableArguments($view);
  if (empty($indexable_arguments)) {
    return FALSE;
  }

  // Check that the number of identifiers is equal to the number of values.
  $args_ids = array_slice($indexable_arguments, 0, count($args));
  if (count($args_ids) != count($args)) {
    return FALSE;
  }

  // Check that the current number of rows in the index does not
  // exceed the specified number.
  $condition = new Condition('AND');
  $condition
    ->condition('view_id', $view
    ->id());
  $condition
    ->condition('display_id', $view->current_display);
  $settings = $this
    ->getSitemapSettings($view);
  $max_links = is_numeric($settings['max_links']) ? $settings['max_links'] : 0;
  if ($max_links > 0 && $this
    ->getArgumentsFromIndexCount($condition) >= $max_links) {
    return FALSE;
  }

  // Convert the set of identifiers and a set of values to string.
  $args_ids = $this
    ->convertArgumentsArrayToString($args_ids);
  $args_values = $this
    ->convertArgumentsArrayToString($args);
  $condition
    ->condition('arguments_ids', $args_ids);
  $condition
    ->condition('arguments_values', $args_values);

  // Check that this set of arguments has not yet been indexed.
  if ($this
    ->getArgumentsFromIndexCount($condition)) {
    return FALSE;
  }

  // Check that the view result is not empty for this set of arguments.
  $params = array_merge([
    $view
      ->id(),
    $view->current_display,
  ], $args);
  $view_result = call_user_func_array('views_get_view_result', $params);
  if (empty($view_result)) {
    return FALSE;
  }

  // Add a set of arguments to the index.
  $options = [
    'return' => Database::RETURN_AFFECTED,
  ];
  $query = $this->database
    ->insert('simple_sitemap_views', $options);
  $query
    ->fields([
    'view_id' => $view
      ->id(),
    'display_id' => $view->current_display,
    'arguments_ids' => $args_ids,
    'arguments_values' => $args_values,
  ]);
  return (bool) $query
    ->execute();
}