1 layout.admin.inc layout_condition_add_form($form, &$form_state, Layout $layout = NULL, Block $block = NULL, LayoutMenuItem $menu_item = NULL, $condition_id = NULL)

Form callback; Displays form for adding new conditions to a layout or block.

Parameters

Layout $layout: The layout to which a condition is being added.

Block|null $block: If applying a condition to a particular block, the block instance.

LayoutMenuItem|null $menu_item: If applying a condition to an menu item, the menu item object.

int|null $condition_id: The integer ID of the condition being configured. If adding a new condition, no value will be passed in. Note that this value may be a FALSE-like value, such as 0, indicating the first condition is being configured, which is different than a NULL value indicating a new condition is being added.

Related topics

File

core/modules/layout/layout.admin.inc, line 2017
Admin page callbacks for the Layout module.

Code

function layout_condition_add_form($form, &$form_state, Layout $layout = NULL, Block $block = NULL, LayoutMenuItem $menu_item = NULL, $condition_id = NULL) {
  form_load_include($form_state, 'inc', 'layout', 'layout.admin');
  $form_state['layout'] = $layout;
  $form_state['block'] = $block;
  $form_state['menu_item'] = $menu_item;
  $form_state['condition_id'] = $condition_id;

  // Condition position may be a zero-value. Use a clear variable for checking
  // the add vs. configure existing status.
  $is_new_condition = TRUE;
  if (isset($condition_id)) {
    $is_new_condition = FALSE;
  }
  // Specify the default value of the current condition if configuring an
  // existing condition.
  if (!isset($form_state['values']['condition'])) {
    $form_state['values']['condition'] = $condition_id;
  }

  $handler = NULL;
  if (isset($menu_item)) {
    $form['#redirect'] = 'admin/structure/layouts/menu/' . $menu_item->name . '/access';
    if ($is_new_condition) {
      $form['#title'] = t('Add condition');
    }
    else {
      $form['#title'] = t('Configure visibility condition');
    }
    $help = t('Restrict access to all layouts at this path.');
    $handler = isset($menu_item->conditions[$condition_id]) ? $menu_item->conditions[$condition_id] : NULL;
  }
  elseif (isset($block)) {
    $form['#redirect'] = 'admin/structure/layouts/manage/' . $layout->name;
    if ($is_new_condition) {
      $form['#title'] = t('Add condition for "!title" block', array('!title' => $block->getAdminTitle()));
    }
    else {
      $form['#title'] = t('Configure visibility condition for "!title" block', array('!title' => $block->getAdminTitle()));
    }
    $help = t('Limit the situations in which this block will be shown.');
    $handler = isset($block->conditions[$condition_id]) ? $block->conditions[$condition_id] : NULL;
  }
  else {
    $form['#redirect'] = 'admin/structure/layouts/manage/' . $layout->name . '/configure';
    if ($is_new_condition) {
      $form['#title'] = t('Add condition for "@title" layout', array('@title' => $layout->title));
    }
    else {
      $form['#title'] = t('Configure visibility condition for "@title" layout', array('@title' => $layout->title));
    }
    $help = t('Limit the situations in which this layout will apply.');
    $handler = isset($layout->conditions[$condition_id]) ? $layout->conditions[$condition_id] : NULL;
  }
  $form['help'] = array(
    '#type' => 'help',
    '#markup' => $help,
  );
  $all_access_info = _layout_get_all_info('layout_access');
  backdrop_sort($all_access_info, array('title'));
  $form['condition'] = array(
    '#type' => 'select',
    '#title' => t('Visibility condition'),
    '#options' => array(),
    '#required' => TRUE,
    '#default_value' => isset($handler->plugin) ? $handler->plugin : NULL,
    '#parents' => array('condition'),
    '#ajax' => array(
      'wrapper' => 'condition_settings',
      'callback' => 'layout_condition_ajax_style',
    ),
  );
  foreach ($all_access_info as $access_info) {
    $required_contexts = !empty($access_info['required contexts']) ? $access_info['required contexts'] : array();
    $layout_has_context = $layout && $layout->hasContexts($required_contexts);
    $menu_item_has_context = $menu_item && $menu_item->hasContexts($required_contexts);
    if (empty($required_contexts) || $layout_has_context || $menu_item_has_context) {
      $form['condition']['#options'][$access_info['name']] = $access_info['title'];
    }
  }

  $form['condition_settings'] = array(
    '#type' => 'container',
    '#id' => 'condition_settings',
    '#parents' => array('condition_settings'),
  );
  $form['condition_settings']['content'] = layout_condition_return_form($form['condition_settings'], $form_state);
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['load_condition_nojs'] = array(
    '#type' => 'submit',
    '#value' => t('Load condition'),
    '#submit' => array(
      'layout_condition_add_load_condition_nojs',
    ),
    '#attributes' => array(
      'class' => array('js-hide'),
    ),
  );
  $form['actions']['add'] = array(
    '#type' => 'submit',
    '#value' => $is_new_condition ? t('Add condition') : t('Save visibility condition'),
    '#submit' => array(
      'layout_condition_add_form_submit',
    ),
    '#validate' => array(
      'layout_condition_add_form_validate',
    ),
    '#ajax' => array(
      'callback' => 'layout_ajax_form_save_dialog',
    ),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#limit_validation_errors' => array(),
    '#submit' => array(
      'layout_condition_add_form_cancel',
    ),
    '#ajax' => array(
      'callback' => 'layout_ajax_condition_cancel_dialog',
    ),
  );
  return $form;
}