5 * Contains \Drupal\php\Plugin\views\argument_default\Php.
8 namespace Drupal\php\Plugin\views\argument_default;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
15 * Default argument plugin to provide a PHP code block.
17 * @ViewsArgumentDefault(
20 * title = @Translation("PHP Code")
23 class Php extends ArgumentDefaultPluginBase {
28 protected function defineOptions() {
29 $options = parent::defineOptions();
30 $options['code'] = ['default' => ''];
38 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
39 parent::buildOptionsForm($form, $form_state);
41 '#type' => 'textarea',
42 '#title' => $this->t('PHP contextual filter code'),
43 '#default_value' => $this->options['code'],
44 '#description' => $this->t('Enter PHP code that returns a value to use for this filter. Do not use <?php ?>. You must return only a single value for just this filter. Some variables are available: the view object will be "$view". The argument handler will be "$argument", for example you may change the title used for substitutions for this argument by setting "argument->validated_title".'),
47 // Only do this if using one simple standard form gadget.
48 $this->checkAccess($form, 'code');
52 * Only let users with PHP block visibility permissions set/modify this
55 public function access() {
56 return \Drupal::currentUser()->hasPermission('use PHP for settings');
62 public function getArgument() {
63 // Set up variables to make it easier to reference during the argument.
65 $argument = &$this->argument;
67 $result = eval($this->options['code']);