You are here

function _ctools_context_converter_selector in Chaos Tool Suite (ctools) 7

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

Helper function for ctools_context_converter_selector().

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

Parameters

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

ctools_context $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|null A form element, or NULL if there are no contexts that satisfy the requirements.

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

File

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

Code

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

    // If there's more than one to choose from, create a select widget.
    $options = array();
    foreach ($filtered as $cid => $context) {
      if ($context->type === 'any') {
        $options[''] = t('No context');
        continue;
      }
      $key = $context
        ->get_identifier();
      if ($converters = ctools_context_get_converters($cid . '.', $context)) {
        $options[$key] = $converters;
      }
    }
    if (empty($options)) {
      return array(
        '#type' => 'value',
        '#value' => 'any',
      );
    }
    if (!empty($required->title)) {
      $title = $required->title;
    }
    else {
      $title = $num ? t('Context %count', array(
        '%count' => $num,
      )) : t('Context');
    }
    return array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => $title,
      '#description' => t('Please choose which context and how you would like it converted.'),
      '#default_value' => $default,
    );
  }
  else {

    // Not enough choices to need a selector, so don't make one.
    return NULL;
  }
}