views_plugin_argument_default_php.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_argument_default_php.
  5. */
  6. /**
  7. * Default argument plugin to provide a PHP code block.
  8. *
  9. * @ingroup views_argument_default_plugins
  10. */
  11. class views_plugin_argument_default_php extends views_plugin_argument_default {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. $options['code'] = array('default' => '');
  18. return $options;
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function options_form(&$form, &$form_state) {
  24. parent::options_form($form, $form_state);
  25. $form['code'] = array(
  26. '#type' => 'textarea',
  27. '#title' => t('PHP contextual filter code'),
  28. '#default_value' => $this->options['code'],
  29. '#description' => t('Enter PHP code that returns a value to use for this filter. Do not use &lt;?php ?&gt;. 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"".'),
  30. );
  31. // Only do this if using one simple standard form gadget.
  32. $this->check_access($form, 'code');
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function convert_options(&$options) {
  38. if (!isset($options['code']) && isset($this->argument->options['default_argument_php'])) {
  39. $options['code'] = $this->argument->options['default_argument_php'];
  40. }
  41. }
  42. /**
  43. * Only let users with PHP block visibility permissions set/modify this
  44. * default plugin.
  45. */
  46. public function access() {
  47. return user_access('use PHP for settings');
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function get_argument() {
  53. // set up variables to make it easier to reference during the argument.
  54. $view = &$this->view;
  55. $argument = &$this->argument;
  56. ob_start();
  57. $result = eval($this->options['code']);
  58. ob_end_clean();
  59. return $result;
  60. }
  61. }