You are here

function panelizer_form_panels_change_layout_alter in Panelizer 7.3

Implements hook_form_FORM_ID_alter() for panels_change_layout().

Alter the change layout form to support saving as default.

File

./panelizer.module, line 1869
The Panelizer module attaches panels to entities, providing default panels and allowing each panel to be configured independently by privileged users.

Code

function panelizer_form_panels_change_layout_alter(&$form, &$form_state) {

  // Only alter on initial form build.
  if (empty($form_state['executed'])) {
    if (isset($form_state['back'])) {

      // Do nothing when moving backwards.
      return;
    }

    // Extract the display from the form state.
    $display = $form_state['display'];
    $cache_key = $display->cache_key;
    $parts = explode(':', $cache_key, 3);
    if ($parts[0] != 'panelizer') {
      return;
    }
    list($module, $type, $key) = $parts;

    // If the display is not owned by Panelizer, or is default handler, exit.
    if ($module != 'panelizer' || $type == 'default') {
      return;
    }

    // Load the $plugin information.
    $handler = panelizer_entity_plugin_get_handler($type);
    if (!$handler) {
      return;
    }

    // Get the entity that's being edited through Panelizer context.
    $entity = $display->context['panelizer']->data;

    // Check if the view mode is configured.
    list($entity_id, $view_mode) = explode(':', $key);
    if (empty($entity->panelizer[$view_mode])) {
      return;
    }
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($handler->entity_type, $entity);
    $entity_info = entity_get_info($handler->entity_type);
    $panelizer = $entity->panelizer[$view_mode];

    // Calculate the proper name to add to the cache key, which has some data
    // stripped off of the true name.
    $name = 'default';
    if (!empty($panelizer->name)) {
      $pieces = explode(':', $panelizer->name);
      if (isset($pieces[2])) {
        $name = $pieces[2];
      }
    }
    module_load_include('inc', 'panelizer', 'includes/common');
    $revision_info = $handler
      ->entity_allows_revisions($entity);

    // If this entity has revisions enabled we can assume that they have
    // permissions to add revisions via IPE.
    if (!empty($revision_info)) {

      // Re-load the entity so we get one from the entity cache, which is
      // necessary for panelizer_panels_cache_save() to get our changes.
      $entities = entity_load($handler->entity_type, array(
        $entity_id,
      ));
      $form_state['entity'] = $entities[$entity_id];
      $form_state['revision info'] = $revision_info;
      panelizer_add_revision_info_form($form, $form_state);

      // Make it clear from description that this only affects custom displays.
      if (isset($form['revision_information']['revision'])) {
        $form['revision_information']['revision']['#description'] = t('This only affects changes saved as custom.');
      }
    }

    // Move the submit buttons down so they'll appear after the revision info.
    $form['back']['#weight'] = 20;
    $form['submit']['#weight'] = 25;

    // Change the default button to say "Save as custom".
    $form['submit']['#value'] = t('Save as custom');

    // Add another button to save as the default instead.
    $form['save_default'] = array(
      '#type' => 'submit',
      '#value' => t('Save as @bundle_name default', array(
        '@bundle_name' => $entity_info['bundles'][$bundle]['label'],
      )),
      '#submit' => array(
        'panels_ipe_change_to_default',
        'panels_change_layout_submit',
      ),
      '#save-display' => TRUE,
      '#cache_key' => 'default:' . $handler->entity_type . ':' . $bundle . '.' . $panelizer->view_mode . ':' . $name,
      '#access' => $handler
        ->panelizer_access('defaults', $entity, $panelizer->view_mode),
      '#weight' => 30,
    );
  }
}