1.20.x layout.context.admin.inc layout_context_relationship_add_form($form, &$form_state, Layout $layout = NULL, $relationship_id = NULL)

Form callback; Displays form for adding new relationships to a layout.

Parameters

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

int|null $relationship_id: The ID of the relationship being configured. If adding a new relationship, no value will be passed in.

Related topics

File

modules/layout/layout.context.admin.inc, line 144
Administrative functions for custom layout contexts.

Code

function layout_context_relationship_add_form($form, &$form_state, Layout $layout = NULL, $relationship_id = NULL) {
  form_load_include($form_state, 'inc', 'layout', 'layout.context.admin');
  form_load_include($form_state, 'inc', 'layout', 'layout.admin');

  $contexts = $layout->getContexts();

  // Get contexts from relationships
  $form_state['relevant_info'] = layout_relationships_get_relevant_info($contexts);

  $relevant_relationships = array();
  $parent_relationship = array();

  foreach ($form_state['relevant_info'] as $rid => $info) {
    $relevant_relationships[$rid] = $info['title'];
  }

  $is_new_context = TRUE;
  if (isset($relationship_id)) {
    $is_new_context = FALSE;
  }

  // Ensure AJAX callbacks display the correct title by setting it here.
  $form['#title'] = $is_new_context ? t('Add relationship') : t('Configure relationship');

  $form_state['layout'] = $layout;
  $form_state['contexts'] = $contexts;
  if (!empty($relationship_id)) {
    $form_state['relationship_id'] = $relationship_id;
  }
  elseif (isset($form_state['values']['context'])) {
    $form_state['relationship_id'] = $form_state['values']['context'];
  }

  $form['help'] = array(
    '#type' => 'help',
    '#markup' => 'Relationships create contexts from data provided by existing contexts, like the author from node context.',
  );

  $current_relationships = $layout->relationships;
  $form_state['relationships'] = $current_relationships;
  if (isset($current_relationships[$relationship_id])) {
    $handler = $current_relationships[$relationship_id];
    $form_state['handler'] = $handler;
  }
  else {
    $handler = NULL;
  }
  if (!empty($relevant_relationships)) {
    backdrop_sort($relevant_relationships, array('title'));
    $form['relationship'] = array(
      '#type' => 'select',
      '#title' => t('Available context relationships'),
      '#options' => $relevant_relationships,
      '#required' => TRUE,
      '#default_value' => !empty($handler) ? $handler->plugin : NULL,
      '#parents' => array('relationship'),
      '#ajax' => array(
        'wrapper' => 'context_settings',
        'callback' => 'layout_context_ajax_style',
      ),
    );
  }
  else {
    $form['no_relationship'] = array(
      '#markup' => t('No relevant relationships available'),
    );
  }

  $form['context_settings'] = array(
    '#type' => 'container',
    '#id' => 'context_settings',
    '#parents' => array('context_settings'),
  );
  $form['context_settings']['content'] = layout_context_return_form($form['context_settings'], $form_state);
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['load_relationship_nojs'] = array(
    '#type' => 'submit',
    '#value' => t('Load relationship'),
    '#submit' => array(
      'layout_context_add_load_context_nojs',
    ),
    '#attributes' => array(
      'class' => array('js-hide'),
    ),
  );
  $form['actions']['add'] = array(
    '#type' => 'submit',
    '#name' => 'relationship_add_button',
    '#access' => !empty($relevant_relationships),
    '#value' => $is_new_context ? t('Add relationship') : t('Save relationship'),
    '#submit' => array(
      'layout_context_add_form_submit',
    ),
    '#validate' => array(
      'layout_context_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_context_add_form_cancel'),
    '#ajax' => array(
      'callback' => 'layout_ajax_context_cancel_dialog',
    ),
  );
  return $form;
}