Php.php in PHP 8
File
src/Plugin/Condition/Php.php
View source
<?php
namespace Drupal\php\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
class Php extends ConditionPluginBase {
public function defaultConfiguration() {
return [
'php' => '<?php return TRUE; ?>',
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['php'] = [
'#type' => 'textarea',
'#title' => $this
->t('When the following PHP return TRUE (experts only)'),
'#default_value' => $this->configuration['php'],
'#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.'),
'#access' => \Drupal::currentUser()
->hasPermission('use PHP for settings'),
];
return parent::buildConfigurationForm($form, $form_state);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['php'] = $form_state
->getValue('php');
parent::submitConfigurationForm($form, $form_state);
}
public function summary() {
if (!empty($this->configuration['php'])) {
return t('When the given PHP evaluates as @state.', [
'@state' => !empty($this->configuration['negate']) ? 'FALSE' : 'TRUE',
]);
}
else {
return t('No PHP code has been provided.');
}
}
public function evaluate() {
return php_eval($this->configuration['php']);
}
}
Classes
Name |
Description |
Php |
Provides a 'Php' condition. |