Further modules included.
[yaffs-website] / web / modules / contrib / php / src / Plugin / Condition / Php.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\php\Plugin\Condition\Php.
6  */
7
8 namespace Drupal\php\Plugin\Condition;
9
10 use Drupal\Core\Condition\ConditionPluginBase;
11 use Drupal\Core\Form\FormStateInterface;
12
13 /**
14  * Provides a 'Php' condition.
15  *
16  * @Condition(
17  *   id = "php",
18  *   label = @Translation("PHP")
19  * )
20  */
21 class Php extends ConditionPluginBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function defaultConfiguration() {
27     // By default the PHP snippet need to return TRUE or blocks will silently
28     // disappear after the module has been enabled and/or a block has been
29     // configured without configuring a PHP snippet.
30     return ['php' => '<?php return TRUE; ?>'] + parent::defaultConfiguration();
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
37     $form['php'] = [
38       '#type' => 'textarea',
39       '#title' => $this->t('When the following PHP return TRUE (experts only)'),
40       '#default_value' => $this->configuration['php'],
41       '#description' => $this->t('Enter PHP code between &lt;?php ?&gt;. Note that executing incorrect PHP code can break your Drupal site. Return TRUE in order for this condition to evaluate as TRUE.'),
42       '#access' => \Drupal::currentUser()->hasPermission('use PHP for settings')
43     ];
44
45     return parent::buildConfigurationForm($form, $form_state);
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
52     $this->configuration['php'] = $form_state->getValue('php');
53     parent::submitConfigurationForm($form, $form_state);
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function summary() {
60     if (!empty($this->configuration['php'])) {
61       return t('When the given PHP evaluates as @state.', ['@state' => !empty($this->configuration['negate']) ? 'FALSE' : 'TRUE']);
62     }
63     else {
64       return t('No PHP code has been provided.');
65     }
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function evaluate() {
72     return php_eval($this->configuration['php']);
73   }
74
75 }