Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / php / src / Plugin / views / argument_default / Php.php
1 <?php
2
3 namespace Drupal\php\Plugin\views\argument_default;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
7
8 /**
9  * Default argument plugin to provide a PHP code block.
10  *
11  * @ViewsArgumentDefault(
12  *   id = "php",
13  *   module = "php",
14  *   title = @Translation("PHP Code")
15  * )
16  */
17 class Php extends ArgumentDefaultPluginBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function defineOptions() {
23     $options = parent::defineOptions();
24     $options['code'] = ['default' => ''];
25
26     return $options;
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
33     parent::buildOptionsForm($form, $form_state);
34     $form['code'] = [
35       '#type' => 'textarea',
36       '#title' => $this->t('PHP contextual filter code'),
37       '#default_value' => $this->options['code'],
38       '#description' => $this->t('Enter PHP code that returns a value to use for this filter. Do not use &lt;?php ?&gt;. 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".'),
39     ];
40
41     // Only do this if using one simple standard form gadget.
42     $this->checkAccess($form, 'code');
43   }
44
45   /**
46    * Permissions check.
47    *
48    * Only let users with PHP block visibility permissions set/modify this
49    * default plugin.
50    */
51   public function access() {
52     return \Drupal::currentUser()->hasPermission('use PHP for settings');
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getArgument() {
59     // Set up variables to make it easier to reference during the argument.
60     $view = &$this->view;
61     $argument = &$this->argument;
62     ob_start();
63     $result = eval($this->options['code']);
64     ob_end_clean();
65     return $result;
66   }
67
68 }