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