1 layout.entity.admin.inc layout_entity_admin_add_form(array $form, array &$form_state, $entity_type, $bundle_arg)

Render the form for adding a layout for an entity.

Parameters

array $form: The form array.

array $form_state: The form state.

string $entity_type: The entity type such as "node", "user", "taxonomy_term", etc.

string|stdClass $bundle_arg: The bundle as a string or object. The object may be a TaxonomyVocabulary or stdClass (in the case of a node type).

Return value

array: The built form array.

Related topics

File

core/modules/layout/layout.entity.admin.inc, line 178
Provides a user interface for managing layouts for entity bundles.

Code

function layout_entity_admin_add_form(array $form, array &$form_state, $entity_type, $bundle_arg) {
  form_load_include($form_state, 'inc', 'layout', 'layout.admin');
  form_load_include($form_state, 'inc', 'layout', 'layout.context.admin');
  backdrop_set_title(layout_entity_menu_title($entity_type, $bundle_arg));

  $config = array(
    'is_new' => TRUE,
  );
  $layout = new Layout($config);

  $form_state['layout'] = &$layout;
  $form_state['entity_type'] = $entity_type;
  $form_state['bundle_type'] = field_extract_bundle($entity_type, $bundle_arg);

  $form['#tree'] = TRUE;

  $form['#attached']['js'][] = backdrop_get_path('module', 'layout') . '/js/layout.admin.js';
  $form['#attached']['css'][] = backdrop_get_path('module', 'layout') . '/css/layout.admin.css';

  $entity_layouts = layout_get_entity_layouts($entity_type);
  $bundle = field_extract_bundle($entity_type, $bundle_arg);
  $bundle_label = _layout_entity_bundle_label($entity_type, $bundle_arg);
  if (isset($entity_layouts['bundles'][$bundle]) && $bundle_count = count($entity_layouts['bundles'][$bundle])) {
    $default_name = t('@bundle_label layout @count', array(
      '@bundle_label' => $bundle_label,
      '@count' => $bundle_count + 1,
    ));
  }
  else {
    $default_name = t('@bundle_label layout', array(
      '@bundle_label' => $bundle_label,
    ));
  }

  $form['title'] = array(
    '#title' => t('Layout name'),
    '#type' => 'textfield',
    '#maxlength' => 128,
    '#default_value' => $default_name,
    '#required' => TRUE,
    '#access' => !$layout->isDefault(),
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#machine_name' => array(
      'source' => array('title'),
      'exists' => 'layout_load',
    ),
    '#maxlength' => 128,
    '#default_value' => $layout->name,
    '#required' => TRUE,
    '#access' => !$layout->isDefault(),
  );
  $form['layout_template'] = array(
    '#title' => t('Layout template'),
    '#type' => 'radios',
    '#default_value' => layout_load('default')->layout_template,
    '#options' => array(),
    '#wrapper_attributes' => array(
      'class' => array('clearfix', 'layout-options'),
    ),
    '#required' => TRUE,
  );

  $fields = field_info_instances($entity_type, $form_state['bundle_type']);
  $field_blocks = array();
  foreach ($fields as $key => $field) {
    $field_blocks[$key] = $field['label'];
  }
  $field_blocks += array('main' => t('Main content block'));

  $first_field = reset($fields);
  $form['content_area'] = array(
    '#title' => t('Field blocks'),
    '#type' => 'radios',
    '#default_value' => 'main_content',
    '#options' => array(
      'main_content' => t('Use the "Main content" block to render fields'),
      'field_blocks' => t('Select individual field blocks'),
    ),
    '#description' => empty($first_field) ? t('There are no fields attached to this entity.') : '',
    '#disabled' => empty($first_field),
  );
  $form['field_blocks'] = array(
    '#title' => t('Available field blocks'),
    '#type' => 'checkboxes',
    '#default_value' => array('main'),
    '#options' => $field_blocks,
    '#states' => array(
      'visible' => array(
        ':input[name="content_area"]' => array('value' => 'field_blocks'),
      ),
    ),
    '#indentation' => 1,
  );

  // Set the new layout weight to be one less than the lowest weight for this
  // path. This ensures that the new layout takes effect, regardless of other
  // conditions that might be in place at the same path.
  $lowest_weight = count($entity_layouts['all']);
  foreach ($entity_layouts['all'] as $entity_layout) {
    /* @var Layout $entity_layout */
    if ($entity_layout->weight < $lowest_weight) {
      $lowest_weight = $entity_layout->weight;
    }
  }
  $form['weight'] = array(
    '#type' => 'hidden',
    '#value' => $lowest_weight - 1,
  );

  // Get the list of layout template options. The list needs to be rebuilt (see
  // https://github.com/backdrop/backdrop-issues/issues/984)
  $all_template_info = layout_get_layout_template_info(NULL, TRUE);

  $excluded = config_get('layout.settings', 'excluded_templates');
  foreach ($all_template_info as $template_name => $template_info) {
    if (!in_array($template_name, $excluded)) {
      $form['layout_template']['#options'][$template_name] = theme('layout_template_option', array('template_info' => $template_info));
    }
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => empty($layout->is_new) ? t('Save layout') : t('Create layout'),
    '#submit' => array(
      'layout_settings_form_update_layout',
      'layout_entity_admin_add_form_submit',
    ),
  );
  if (isset($_SESSION['layout_new_name']) || isset($layout->locked)) {
    $form['actions']['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#limit_validation_errors' => array(array('actions', 'reset')),
      '#validate' => array(),
      '#submit' => array(
        'layout_settings_form_reset',
      ),
    );
  }

  return $form;
}