Further modules included.
[yaffs-website] / web / modules / contrib / php / src / Plugin / views / argument_validator / Php.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\views\Plugin\views\argument_validator\Php.
6  */
7
8 namespace Drupal\php\Plugin\views\argument_validator;
9
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase;
12
13
14 /**
15  * Provide PHP code to validate whether or not an argument is ok.
16  *
17  * @ViewsArgumentValidator(
18  *   id = "php",
19  *   module = "php",
20  *   title = @Translation("PHP Code")
21  * )
22  */
23 class Php extends ArgumentValidatorPluginBase {
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 validate code'),
43       '#default_value' => $this->options['code'],
44       '#description' => $this->t('Enter PHP code that returns TRUE or FALSE. No return is the same as FALSE, so be SURE to return something if you do not want to declare the argument invalid. Do not use &lt;?php ?&gt;. The argument to validate will be "$argument" and the view will be "$view". You may change the argument by setting "$handler->argument". You may change the title used for substitutions for this argument by setting "$handler->validated_title".'),
45     ];
46
47     $this->checkAccess($form, 'code');
48   }
49
50   /**
51    * Only let users with PHP block visibility permissions set/modify this
52    * validate plugin.
53    */
54   public function access() {
55     return \Drupal::currentUser()->hasPermission('use PHP for settings');
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function validateArgument($argument) {
62     // Set up variables to make it easier to reference during the argument.
63     $view = &$this->view;
64     $handler = &$this->argument;
65
66     ob_start();
67     $result = eval($this->options['code']);
68     ob_end_clean();
69     return $result;
70   }
71
72 }