You are here

function _ctools_context_selector in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 includes/context.inc \_ctools_context_selector()

Helper function for ctools_context_selector().

@internal This function DOES NOT form part of the CTools API. Use the API function ctools_context_selector() instead.

Parameters

array $contexts: A keyed array of all available contexts.

ctools_context_required|ctools_context_optional $required: The required context object.

$default: The default value for the select object, suitable for a #default_value render key.

int $num: If supplied and non-zero, the title of the select form element will be "Context $num", otherwise it will be "Context".

Return value

array A form element, or NULL if there are no contexts that satisfy the requirements.

1 call to _ctools_context_selector()
ctools_context_selector in includes/context.inc
Create a select box to choose possible contexts.

File

includes/context.inc, line 591
Contains code related to the ctools system of 'context'.

Code

function _ctools_context_selector($contexts, $required, $default, $num = 0) {
  $filtered = ctools_context_filter($contexts, $required);
  $count = count($filtered);
  $form = array();
  if ($count >= 1) {

    // If there's more than one to choose from, create a select widget.
    foreach ($filtered as $cid => $context) {
      $options[$cid] = $context
        ->get_identifier();
    }
    if (!empty($required->title)) {
      $title = $required->title;
    }
    else {
      $title = $num ? t('Context %count', array(
        '%count' => $num,
      )) : t('Context');
    }
    $form = array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => $title,
      '#default_value' => $default,
    );
  }
  return $form;
}