You are here

public static function InsertView::build in Advanced Insert View 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Filter/InsertView.php \Drupal\insert_view_adv\Plugin\Filter\InsertView::build()

Builds the view markup from the data received from the token.

Parameters

string $view_name: The machine name of the view.

string $display_id: The name of the display to show.

string $args: The arguments that are passed to the view in format arg1/arg2/arg3/... .

string $configuration: Json encoded string of the filter configuration.

Return value

array The rendered array of the view to display.

1 call to InsertView::build()
AdvancedInsertViewController::ajaxView in src/Controller/AdvancedInsertViewController.php
Loads and renders a view via AJAX.

File

src/Plugin/Filter/InsertView.php, line 120

Class

InsertView
Provides a filter for insert view.

Namespace

Drupal\insert_view_adv\Plugin\Filter

Code

public static function build($view_name, $display_id, $args, $configuration) {
  $plain = '';

  // Just in case check if this is an array already.
  if (!is_array($configuration)) {
    $configuration = Json::decode($configuration);
  }

  // Check what to do if the render array is empty and there is nothing to show.
  if ($configuration && isset($configuration['settings']['render_as_empty']) && $configuration['settings']['render_as_empty'] == 0) {
    $plain = '[view:' . $view_name . '=' . $display_id;
    if (!empty($args)) {
      $plain .= '=' . $args;
    }
    $plain .= ']';
  }

  // Do nothing if there is no view name provided.
  if (empty($view_name)) {
    return [
      '#attached' => [],
      '#markup' => $plain,
    ];
  }

  // Do not render the views that are not allowed to be printed.
  if ($configuration && !empty($configuration['settings']['allowed_views'])) {
    $allowed_views = array_filter($configuration['settings']['allowed_views']);
    if (!empty($allowed_views) && empty($allowed_views[$view_name . '=' . $display_id])) {
      return [
        '#attached' => [],
        '#markup' => $plain,
      ];
    }
  }

  /** @var \Drupal\views\ViewExecutable $view */
  $view = Views::getView($view_name);
  if (empty($view)) {
    return [
      '#attached' => [],
      '#markup' => $plain,
    ];
  }

  // Check if the current user has access to the given view.
  if (!$view
    ->access($display_id)) {
    return [
      '#attached' => [],
      '#markup' => $plain,
    ];
  }
  $view
    ->setDisplay($display_id);

  /** @var \Symfony\Component\HttpFoundation\Request $request */
  $request = \Drupal::service('request_stack')
    ->getCurrentRequest();
  $current_path = $request
    ->getPathInfo();

  // Workaround for exposed filter reset button.
  // Because of exposed form redirect on reset, lazyloading throws
  // Drupal\Core\Form\EnforcedResponseException. For this reason we need to
  // perform this redirect with javascript without rendering the form.
  if (\Drupal::currentUser()
    ->isAuthenticated() && \Drupal::moduleHandler()
    ->moduleExists('big_pipe')) {
    $op = $request
      ->get('op');
    $display_options = $view->display_handler
      ->getOption('exposed_form');
    if (!is_null($op) && !empty($display_options) && $op == $display_options['options']['reset_button_label']) {
      return [
        '#attached' => [
          'drupalSettings' => [
            'insert_view_adv' => [
              'reset_redirect' => $current_path,
            ],
          ],
          'library' => [
            'insert_view_adv/reset_redirect',
          ],
        ],
      ];
    }
  }

  // Try to get the arguments from the current path.
  $url_args = explode('/', $current_path);
  foreach ($url_args as $id => $arg) {
    $args = str_replace("%{$id}", $arg, $args);
  }
  $args = preg_replace(',/?(%\\d),', '', $args);
  $args = $args ? explode('/', $args) : [];
  return $view
    ->preview($display_id, $args) ?: [];
}