You are here

function panelizer_form_panels_ipe_edit_control_form_alter in Panelizer 7.3

Implements hook_form_FORM_ID_alter() for panels_ipe_edit_control_form().

Alter the IPE save control form to handle extra Panelizer functionality.

File

./panelizer.module, line 1745
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_ipe_edit_control_form_alter(&$form, &$form_state) {
  if (empty($form_state['renderer'])) {
    return;
  }
  $renderer = $form_state['renderer'];
  $cache_key = $renderer->display->cache_key;
  $parts = explode(':', $cache_key, 3);
  if ($parts[0] != 'panelizer') {
    return;
  }
  list($module, $type, $key) = $parts;
  if ($type == 'default') {
    return;
  }

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

  // Get the entity that's being edited.
  list($entity_id, $view_mode) = explode(':', $key);
  $entities = entity_load($handler->entity_type, array(
    $entity_id,
  ));
  if (empty($entities[$entity_id])) {
    return;
  }
  $entity = $entities[$entity_id];
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($handler->entity_type, $entity);
  $entity_info = entity_get_info($handler->entity_type);
  if (empty($entity->panelizer[$view_mode])) {
    return;
  }
  $panelizer = $entities[$entity_id]->panelizer[$view_mode];
  module_load_include('inc', 'panelizer', 'includes/common');
  $revision_info = $handler
    ->entity_allows_revisions($entity);

  // Set panelizer info regardless of revision or panelizer layout default
  // status.
  $form_state['panelizer entity'] = $entity;
  $form_state['panelizer bundle'] = $bundle;
  $form_state['panelizer handler'] = $handler;
  $form_state['panelizer view_mode'] = $view_mode;

  // If this entity has revisions enabled we can assume that they have
  // permissions to add revisions via IPE.
  if (!empty($revision_info)) {
    $form_state['entity'] = $entity;
    $form_state['revision info'] = $revision_info;
    panelizer_add_revision_info_form($form, $form_state);

    // Tweak the log message field to make it fit better.
    if (isset($form['revision_information']['log'])) {
      $form['revision_information']['log']['#type'] = 'textfield';
      $form['revision_information']['log']['#description'] = '';
    }
  }

  // If the entity already has customized panelizer data, add the revert button.
  if (!empty($panelizer->did)) {
    if ($handler
      ->has_default_panel($bundle . '.' . $view_mode)) {
      $uri = entity_uri($handler->entity_type, $entity);
      $url = $uri['path'];

      // Support for Workbench Moderation to redirect to the newest revision for
      // this node.
      if ($handler->entity_type == 'node' && module_exists('workbench_moderation')) {
        $url = 'node/' . $entity->nid . '/current-revision';
      }
      $redirect_url = url($uri['path'] . '/panelizer/' . $view_mode . '/reset', array(
        'query' => array(
          'destination' => $url,
        ),
      ));
      drupal_add_js(array(
        'panelizer' => array(
          'revert_default_url' => $redirect_url,
        ),
      ), 'setting');
      $form['buttons']['revert_default'] = array(
        '#type' => 'submit',
        '#value' => t('Revert to @bundle_name default', array(
          '@bundle_name' => $entity_info['bundles'][$bundle]['label'],
        )),
        '#submit' => array(
          'panels_ipe_revert_to_default',
        ),
        '#attributes' => array(
          'class' => array(
            'panels-ipe-cancel',
          ),
        ),
        '#id' => 'panelizer-ipe-revert',
        '#access' => $handler
          ->panelizer_access('defaults', $entity, $panelizer->view_mode),
      );
    }
    return;
  }

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

  // 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];
    }
  }

  // Add another button to save as the default instead.
  $form['buttons']['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_edit_display_form_submit',
    ),
    '#save-display' => TRUE,
    '#weight' => -1,
    // Precalculate the cache key so we don't have to unwind a bunch of stuff in
    // the submit handler.
    '#cache_key' => 'default:' . $handler->entity_type . ':' . $bundle . '.' . $panelizer->view_mode . ':' . $name,
    '#attributes' => array(
      'class' => array(
        'panels-ipe-save',
      ),
    ),
    '#id' => 'panelizer-save-default',
    '#access' => $handler
      ->panelizer_access('defaults', $entity, $panelizer->view_mode),
  );
}