3 namespace Drupal\php\Plugin\Condition;
5 use Drupal\Core\Condition\ConditionPluginBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Provides a 'Php' condition.
13 * label = @Translation("PHP")
16 class Php extends ConditionPluginBase {
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();
31 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
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 <?php ?>. 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'),
40 return parent::buildConfigurationForm($form, $form_state);
46 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
47 $this->configuration['php'] = $form_state->getValue('php');
48 parent::submitConfigurationForm($form, $form_state);
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']);
59 return t('No PHP code has been provided.');
66 public function evaluate() {
67 return php_eval($this->configuration['php']);