1.20.x layout.admin.inc layout_block_configure_form($form, &$form_state, Layout $layout, Block $block, $renderer_name, $region_name = NULL)

Form callback; Add or edit a block within a layout.

Related topics

File

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

Code

function layout_block_configure_form($form, &$form_state, Layout $layout, Block $block, $renderer_name, $region_name = NULL) {
  form_load_include($form_state, 'inc', 'layout', 'layout.admin');

  $form_state['layout'] = $layout;
  $form_state['block'] = $block;
  if (!isset($form_state['renderer'])) {
    $form_state['renderer'] = layout_create_renderer($renderer_name, $layout);
  }
  if (!isset($region_name)) {
    $region_name = $layout->getBlockPosition($block->uuid);
  }

  if (empty($block->uuid)) {
    $form['#title'] = t('Add block "!title"', array('!title' => $block->getAdminTitle()));
  }
  else {
    $form['#title'] = t('Configure block "!title"', array('!title' => $block->getAdminTitle()));
  }

  $template_info = layout_get_layout_template_info($layout->layout_template);
  $form['region'] = array(
    '#type' => 'select',
    '#title' => t('Region'),
    '#default_value' => $region_name,
    '#options' => $template_info['regions'],
    '#access' => backdrop_is_html(), // Only show this on non-JS requests.
  );

  // We overload the layout object with an "in_progress" key to hold information
  // about the block currently being edited, this eases the multi-dialog
  // workflow for a sequence of dialogs affecting existing or new blocks.
  if (!isset($layout->in_progress['block']) || $layout->in_progress['block']->uuid !== $block->uuid) {
    $layout->in_progress = array(
      'block' => $block,
      'region_name' => $region_name,
      'renderer_name' => $renderer_name,
      'section' => NULL,
    );
  }
  // If resuming an in-progress edit, pull out the region name.
  else {
    $form['region']['#default_value'] = $layout->in_progress['region_name'];
  }

  $form['actions']['#weight'] = 999;
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => empty($block->uuid) ? t('Add block') : t('Update block'),
  );

  $form['#tree'] = TRUE;
  $block->form($form, $form_state);

  /* Style settings: */
  $form['style'] = array(
    '#type' => 'fieldset',
    '#title' => t('Style settings'),
    '#collapsed' => $layout->in_progress['section'] !== 'style',
    '#collapsible' => TRUE,
    '#weight' => 10,
  );
  $styles = _layout_get_all_info('layout_style');
  $style_options = array();
  foreach ($styles as $style_name => $style_info) {
    $style_options[$style_name] = $style_info['title'];
  }
  $current_style = $block->settings['style'];
  if (isset($form_state['values']['style'])) {
    $current_style = $form_state['values']['style'];
  }
  $form['style']['style'] = array(
    '#type' => 'select',
    '#title' => t('Style'),
    '#options' => $style_options,
    '#default_value' => $current_style,
    '#ajax' => array(
      'wrapper' => 'block_style_settings',
      'callback' => 'layout_block_configure_ajax_style'
    ),
    '#parents' => array('style'),
  );
  $form['style']['style_settings'] = array(
    '#type' => 'container',
    '#id' => 'block_style_settings',
    '#parents' => array('style_settings'),
  );

  // The style may change in a form rebuild, so we can't use $block->style
  // directly, instead crate a new style handler and use that form.
  $block->style = layout_create_handler('layout_style', $current_style, $block->style->toArray());
  $block->style->form($form['style']['style_settings'], $form_state);
  /* End style settings. */

  /* Visibility conditions: */
  $form['conditions'] = array(
    '#title' => t('Visibility conditions'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => $layout->in_progress['section'] !== 'conditions',
    '#description' => t('Visibility conditions allow this block to be shown only in certain situations, such as for certain roles or types of content.'),
    '#id' => 'block-access',
    '#weight' => 20,
  );
  $form['conditions']['links'] = array(
    '#theme' => 'layout_action_links',
  );
  $form['conditions']['links']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add visibility condition'),
    '#attributes' => array('class' => array('layout-link-button', 'layout-access-add')),
    '#submit' => array(
      'layout_block_configure_form_submit',
      'layout_block_configure_form_condition_add',
    ),
    '#save_in_progress' => TRUE, // See layout_block_configure_form_submit().
    '#ajax' => array(
      'callback' => 'layout_ajax_form_open_dialog',
    ),
  );
  $form['conditions']['active'] = array(
    '#type' => 'container',
    '#theme' => 'layout_conditions',
    '#attributes' => array('class' => array('layout-access-list')),
  );
  foreach ($block->conditions as $access_key => $block_access) {
    $form['conditions']['active'][$access_key] = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('layout-condition')),
      '#id' => NULL,
    );
    $form['conditions']['active'][$access_key]['label'] = array(
      '#markup' => $block_access->summary(),
    );
    $form['conditions']['active'][$access_key]['remove'] = array(
      '#type' => 'submit',
      '#value' => t('Remove'),
      '#attributes' => array('class' => array('layout-link-button', 'layout-access-remove')),
      '#submit' => array(
        'layout_block_configure_form_condition_remove',
      ),
      '#ajax' => array(
        'callback' => 'layout_ajax_form_update',
      ),
      '#name' => 'conditions' . $access_key . 'remove',
    );
    $form['conditions']['active'][$access_key]['configure'] = array(
      '#type' => 'submit',
      '#value' => t('Configure'),
      '#attributes' => array('class' => array('layout-link-button', 'layout-access-configure')),
      '#submit' => array(
        'layout_block_configure_form_condition_edit',
      ),
      '#save_in_progress' => TRUE, // See layout_block_configure_form_submit().
      '#ajax' => array(
        'callback' => 'layout_ajax_form_open_dialog',
      ),
      '#name' => 'conditions' . $access_key . 'remove',
    );
  }
  /* End visibility conditions. */

  // Note that we specifically use backdrop_is_ajax() in addition to
  // backdrop_is_dialog() to ensure the #ajax callback is specified when the
  // style dropdown rebuilds the form on changes.
  if (backdrop_is_dialog() || backdrop_is_ajax()) {
    $form['actions']['submit']['#ajax'] = array(
      'callback' => 'layout_block_configure_ajax_update',
    );
  }

  return $form;
}