1 layout.entity.admin.inc layout_entity_admin_add_form_submit($form, &$form_state)

Submit handler for layout_entity_admin_add_form().

File

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

Code

function layout_entity_admin_add_form_submit($form, &$form_state) {
  /* @var Layout $layout */
  $layout = $form_state['layout'];
  if (isset($_SESSION['layout_new_name'])) {
    unset($_SESSION['layout_new_name']);
  }

  $entity_type = $form_state['entity_type'];
  $bundle_type = $form_state['bundle_type'];

  // Build the expected path string and set the path.
  $all_info = _layout_get_all_info('layout_context');
  $entity_path_type = $all_info[$entity_type]['menu paths'][0];
  $entity_path = preg_replace('/%[a-z0-9_]+/', '%', $entity_path_type);
  $layout->setPath($entity_path);
  $layout->weight = $form_state['values']['weight'];

  // Set the bundle type visibility condition if the entity has bundles.
  $entity_info = entity_get_info($entity_type);
  $single_bundle = (count($entity_info['bundles']) == 1) && isset($entity_info['bundles'][$entity_type]);
  if (!$single_bundle) {
    $key = $entity_type . '_' . $entity_info['entity keys']['bundle'];
    $handler = layout_create_access($key);
    $handler->settings['bundles'][] = $bundle_type;
    $layout->conditions[] = $handler;
  }

  // If this is a new layout, populate blocks from default layout.
  if (!empty($layout->is_new)) {
    $default_layout_name = path_is_admin($layout->getPath()) ? 'admin_default' : 'default';
    $default_layout = layout_load($default_layout_name);
    // Get the template being used by this layout.
    $template_info = layout_get_layout_template_info($layout->layout_template);
    foreach ($default_layout->positions as $region => $region_block_list) {
      if (array_key_exists($region, $template_info['regions'])) {
        foreach ($region_block_list as $uuid) {
          if (isset($default_layout->content[$uuid])) {
            $block = $default_layout->content[$uuid];
            $new_block = $block->getClone();
            // Only add the block from the default if required contexts are
            // present for each block.
            if ($layout->hasContexts($new_block->getRequiredContexts())) {
              $layout->positions[$region][] = $new_block->uuid;
              $layout->content[$new_block->uuid] = $new_block;
            }
          }
        }
      }
    }

    if ($form_state['values']['content_area'] == 'field_blocks') {
      $fields = field_info_instances($entity_type, $bundle_type);
      $first_field = reset($fields);
      if (!empty($first_field)) {
        $content_region = '';
        if (isset($template_info['regions']['content'])) {
          $content_region = 'content';
        }
        else {
          $template_regions = array_keys($template_info['regions']);
          $content_region = end($template_regions);
        }

        foreach ($fields as $field) {
          if ($form_state['values']['field_blocks'][$field['field_name']]) {
            $key = 'field:field_block:' . $field['entity_type'] . '-' . $field['field_name'];
            $field_block = layout_create_handler('block', $key);
            $build_state = array(
              'build_info' => array('args' => array(
                $layout,
                $field_block,
                'standard',
                $content_region,
              )),
            );
            $build = backdrop_build_form('layout_block_configure_form', $build_state);
            form_execute_handlers('validate', $build, $build_state);
            form_execute_handlers('submit', $build, $build_state);
          }
        }
        if (!$form_state['values']['field_blocks']['main']) {
          $main_content_uuid = '';
          foreach ($layout->content as $uuid => $block) {
            if ($block->module === 'system' && $block->delta === 'main') {
              $main_content_uuid = $uuid;
              break;
            }
          }
          if ($main_content_uuid) {
            $layout->removeBlock($main_content_uuid);
          }
        }
      }
    }

  }

  layout_set_layout_tempstore($layout);

  layout_locked_message($layout, 'layout');

  $form_state['redirect'] = 'admin/structure/layouts/manage/' . $layout->name;
}