You are here

class contextphp_condition_php in Context PHP 7

Same name and namespace in other branches
  1. 6 plugins/contextphp_condition_php.inc \contextphp_condition_php

PHP code as a Context condition.

Hierarchy

Expanded class hierarchy of contextphp_condition_php

2 string references to 'contextphp_condition_php'
contextphp_context_plugins in ./contextphp.module
Implementation of hook_context_plugins().
contextphp_context_registry in ./contextphp.module
Implementation of hook_context_registry().

File

plugins/contextphp_condition_php.inc, line 11
contextphp_condition_php.inc Adds php argument condition to Context

View source
class contextphp_condition_php extends context_condition {
  function condition_values() {
    return array(
      1 => t('True'),
    );
  }

  /**
   * Our value is always TRUE. The PHP code in the options form descides if it
   * validates or not on runtime.
   */
  function condition_form($context) {
    $form['#type'] = 'value';
    $form['#value'] = TRUE;
    return $form;
  }
  function condition_form_submit($values) {
    return array(
      $values,
    );
  }
  function options_form($context) {
    $defaults = $this
      ->fetch_from_context($context, 'options');
    return array(
      'phpcode' => array(
        '#type' => 'textarea',
        '#title' => t('PHP code'),
        '#description' => t('Enter PHP code that returns TRUE if the condition shall be met. Do not use <?php ?>.'),
        '#default_value' => isset($defaults['phpcode']) ? $defaults['phpcode'] : '',
      ),
    );
  }
  function execute() {
    foreach (context_enabled_contexts() as $context) {
      $options = $this
        ->fetch_from_context($context, 'options');
      if (!empty($options['phpcode'])) {
        $code = '<?php ' . $options['phpcode'] . ' ?>';
        if (module_exists('php')) {
          $return = php_eval($code);
        }
        else {
          drupal_set_message(t("Please enable 'PHP filter' to allow the 'Context PHP' module to evaluate your code."), 'warning', TRUE);
          return FALSE;
        }
        if ($return == TRUE) {
          $this
            ->condition_met($context, $return);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
contextphp_condition_php::condition_form function Our value is always TRUE. The PHP code in the options form descides if it validates or not on runtime. Overrides context_condition::condition_form
contextphp_condition_php::condition_form_submit function Condition form submit handler. Overrides context_condition::condition_form_submit
contextphp_condition_php::condition_values function Condition values. Overrides context_condition::condition_values
contextphp_condition_php::execute function
contextphp_condition_php::options_form function Options form. Provide additional options for your condition. Overrides context_condition::options_form
context_condition::$description property
context_condition::$plugin property
context_condition::$title property
context_condition::$values property
context_condition::condition_met function Marks a context as having met this particular condition.
context_condition::condition_used function Check whether this condition is used by any contexts. Can be used to prevent expensive condition checks from being triggered when no contexts use this condition.
context_condition::editor_form function Context editor form for conditions. 2
context_condition::editor_form_submit function Context editor form submit handler.
context_condition::fetch_from_context function Retrieve options from the context provided.
context_condition::get_contexts function Retrieve all contexts with the condition value provided. 2
context_condition::options_form_submit function Options form submit handler.
context_condition::settings_form function Settings form. Provide variable settings for your condition.
context_condition::__clone function Clone our references when we're being cloned.
context_condition::__construct function Constructor. Do not override.