You are here

public function SimpleSitemapViews::addArgumentsToIndexByVariant in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 modules/simple_sitemap_views/src/SimpleSitemapViews.php \Drupal\simple_sitemap_views\SimpleSitemapViews::addArgumentsToIndexByVariant()

Adds view arguments to the index by the sitemap variant.

Parameters

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

string $variant: The name of the sitemap variant.

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

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

Return value

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

Throws

\Exception

1 call to SimpleSitemapViews::addArgumentsToIndexByVariant()
SimpleSitemapViews::addArgumentsToIndex in modules/simple_sitemap_views/src/SimpleSitemapViews.php
Adds view arguments to the index.

File

modules/simple_sitemap_views/src/SimpleSitemapViews.php, line 277

Class

SimpleSitemapViews
Class to manage sitemap data for views.

Namespace

Drupal\simple_sitemap_views

Code

public function addArgumentsToIndexByVariant(ViewExecutable $view, string $variant, array $args, ?string $display_id = NULL) : bool {

  // 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, $variant);
  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 = Database::getConnection()
    ->condition('AND');
  $condition
    ->condition('view_id', $view
    ->id());
  $condition
    ->condition('display_id', $view->current_display);
  $settings = $this
    ->getSitemapSettings($view, $variant);
  $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();
}