3 namespace Drupal\php\Plugin\views\argument_default;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
9 * Default argument plugin to provide a PHP code block.
11 * @ViewsArgumentDefault(
14 * title = @Translation("PHP Code")
17 class Php extends ArgumentDefaultPluginBase {
22 protected function defineOptions() {
23 $options = parent::defineOptions();
24 $options['code'] = ['default' => ''];
32 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
33 parent::buildOptionsForm($form, $form_state);
35 '#type' => 'textarea',
36 '#title' => $this->t('PHP contextual filter code'),
37 '#default_value' => $this->options['code'],
38 '#description' => $this->t('Enter PHP code that returns a value to use for this filter. Do not use <?php ?>. You must return only a single value for just this filter. Some variables are available: the view object will be "$view". The argument handler will be "$argument", for example you may change the title used for substitutions for this argument by setting "argument->validated_title".'),
41 // Only do this if using one simple standard form gadget.
42 $this->checkAccess($form, 'code');
48 * Only let users with PHP block visibility permissions set/modify this
51 public function access() {
52 return \Drupal::currentUser()->hasPermission('use PHP for settings');
58 public function getArgument() {
59 // Set up variables to make it easier to reference during the argument.
61 $argument = &$this->argument;
63 $result = eval($this->options['code']);