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