5 * Contains \Drupal\php\Plugin\Condition\Php.
8 namespace Drupal\php\Plugin\Condition;
10 use Drupal\Core\Condition\ConditionPluginBase;
11 use Drupal\Core\Form\FormStateInterface;
14 * Provides a 'Php' condition.
18 * label = @Translation("PHP")
21 class Php extends ConditionPluginBase {
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();
36 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
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 <?php ?>. 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')
45 return parent::buildConfigurationForm($form, $form_state);
51 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
52 $this->configuration['php'] = $form_state->getValue('php');
53 parent::submitConfigurationForm($form, $form_state);
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']);
64 return t('No PHP code has been provided.');
71 public function evaluate() {
72 return php_eval($this->configuration['php']);