1 layout.module layout_set_handler_contexts(array &$contexts, array $all_contexts, array $required_contexts, array $context_settings)

Helper function for setting contexts on a layout handler.


array $contexts: An array of contexts that will be set by reference if all required contexts are found.

LayoutContext[] $all_contexts: The full array of contexts on an entire layout, from which the required contexts will be selected.

array $required_contexts: The array of context keys and types that need to be selected from the $all_contexts array and populated into the $contexts array.

array $context_settings: If a plugin has multiple contexts, a user may select which context should be used. This array will be keyed by the plugin's required context keys, with the values being the overall layout's context keys.

Return value

bool: TRUE if all contexts were set on the Layout. If a requested context is not available, FALSE will be returned and no contexts will be set at all.


core/modules/layout/layout.module, line 2186
The Layout module creates pages and wraps existing pages in layouts.


function layout_set_handler_contexts(array &$contexts, array $all_contexts, array $required_contexts, array $context_settings) {
  foreach ($required_contexts as $required_context_key => $context_type) {
    // Use the configured context if available.
    if (isset($context_settings[$required_context_key])) {
      $layout_context_key = $context_settings[$required_context_key];
      if (isset($all_contexts[$layout_context_key]) && ($all_contexts[$layout_context_key]->isA($context_type))) {
        $contexts[$required_context_key] = $all_contexts[$layout_context_key];

    // Otherwise use the first context that matches the right type.
    foreach ($all_contexts as $layout_context) {
      if ($layout_context->isA($context_type)) {
        $contexts[$required_context_key] = $layout_context;
        continue 2;

    // If we reach here, no matching required context was found, empty all
    // contexts to prevent partial functionality and return FALSE.
    $contexts = array();
    return FALSE;

  // All required contexts populated successfully.
  return TRUE;