You are here

ViewsReferenceArgument.php in Views Reference Field 8.2

File

src/Plugin/ViewsReferenceSetting/ViewsReferenceArgument.php
View source
<?php

namespace Drupal\viewsreference\Plugin\ViewsReferenceSetting;

use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\views\ViewExecutable;
use Drupal\viewsreference\Plugin\ViewsReferenceSettingInterface;

/**
 * The views reference setting argument plugin.
 *
 * @ViewsReferenceSetting(
 *   id = "argument",
 *   label = @Translation("Argument"),
 *   default_value = "",
 * )
 */
class ViewsReferenceArgument extends PluginBase implements ViewsReferenceSettingInterface {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function alterFormField(array &$form_field) {
    $form_field['#weight'] = 40;
  }

  /**
   * {@inheritdoc}
   */
  public function alterView(ViewExecutable $view, $value) {
    if (!empty($value)) {
      $arguments = [
        $value,
      ];
      if (preg_match('/\\//', $value)) {
        $arguments = explode('/', $value);
      }
      $node = \Drupal::routeMatch()
        ->getParameter('node');
      $token_service = \Drupal::token();
      if (is_array($arguments)) {
        foreach ($arguments as $index => $argument) {
          if (!empty($token_service
            ->scan($argument))) {
            $arguments[$index] = $token_service
              ->replace($argument, [
              'node' => $node,
            ]);
          }
        }
      }
      $view
        ->setArguments($arguments);
    }
  }

}

Classes

Namesort descending Description
ViewsReferenceArgument The views reference setting argument plugin.