You are here

public function PanelizerEntityDefault::add_bundle_setting_form_submit in Panelizer 7.3

Same name and namespace in other branches
  1. 7.2 plugins/entity/PanelizerEntityDefault.class.php \PanelizerEntityDefault::add_bundle_setting_form_submit()

Submit callback for the bundle edit form.

File

plugins/entity/PanelizerEntityDefault.class.php, line 1201
Base class for the Panelizer Entity plugin.

Class

PanelizerEntityDefault
Base class for the Panelizer Entity plugin.

Code

public function add_bundle_setting_form_submit($form, &$form_state, $bundle, $type_location) {

  // Some types do not support changing bundles, so we don't check if it's
  // not possible to change.
  if ($type_location) {
    $new_bundle = drupal_array_get_nested_value($form_state['values'], $type_location);
  }
  else {
    $new_bundle = $bundle;
  }

  // Check to see if the bundle has changed. If so, we need to move stuff
  // around.
  if ($bundle && $new_bundle != $bundle) {

    // Remove old settings.
    variable_del('panelizer_defaults_' . $this->entity_type . '_' . $bundle);
    $allowed_layouts = variable_get('panelizer_' . $this->entity_type . ':' . $bundle . '_allowed_layouts', NULL);
    if ($allowed_layouts) {
      variable_del('panelizer_' . $this->entity_type . ':' . $bundle . '_allowed_layouts');
      variable_set('panelizer_' . $this->entity_type . ':' . $new_bundle . '_allowed_layouts', $allowed_layouts);
    }
    $default = variable_get('panelizer_' . $this->entity_type . ':' . $bundle . '_default', NULL);
    if ($default) {
      variable_del('panelizer_' . $this->entity_type . ':' . $bundle . '_default');
      variable_set('panelizer_' . $this->entity_type . ':' . $new_bundle . '_default', $default);
    }

    // Load up all panelizer defaults for the old bundle and resave them
    // for the new bundle.
    $panelizer_defaults = $this
      ->get_default_panelizer_objects($bundle);
    if (!empty($panelizer_defaults)) {
      foreach ($panelizer_defaults as $panelizer) {
        list($entity_type, $old_bundle, $name) = explode(':', $panelizer->name);
        $panelizer->name = implode(':', array(
          $entity_type,
          $new_bundle,
          $name,
        ));
        if ($panelizer->view_mode != 'page_manager') {
          $panelizer->name .= ':' . $panelizer->view_mode;
        }

        // The default display selection.
        $old_variable_name = 'panelizer_' . $this->entity_type . ':' . $bundle . ':' . $panelizer->view_mode . '_selection';
        $new_variable_name = 'panelizer_' . $this->entity_type . ':' . $new_bundle . ':' . $panelizer->view_mode . '_selection';
        $default_layout = variable_get($old_variable_name, NULL);
        if (!is_null($default_layout)) {
          variable_set($new_variable_name, $default_layout);
          variable_del($old_variable_name);
        }
        $panelizer->panelizer_key = $new_bundle;

        // If there's a pnid this should change the name and retain the pnid.
        // If there is no pnid this will create a new one in the database
        // because exported panelizer defaults attached to a bundle will have
        // to be moved to the database in order to follow along and then be
        // re-exported.
        // @todo Should we warn the user about this?
        ctools_export_crud_save('panelizer_defaults', $panelizer);
      }
    }
  }

  // Fix the configuration.
  // If the main configuration is disabled then everything gets disabled.
  if (empty($form_state['values']['panelizer']['status'])) {
    $form_state['values']['panelizer']['view modes'] = array();
  }
  elseif (!empty($form_state['values']['panelizer']['view modes'])) {

    // Make sure each setting is disabled if the view mode is disabled.
    foreach ($form_state['values']['panelizer']['view modes'] as $view_mode => &$config) {
      if (empty($config['status'])) {
        foreach ($config as $key => $val) {
          $config[$key] = 0;
        }
      }
    }
  }

  // Save the default display for this bundle to a variable so that it may be
  // controlled separately.
  foreach ($this
    ->get_default_panelizer_objects($new_bundle) as $panelizer) {
    if (isset($form_state['values']['panelizer']['view modes'][$panelizer->view_mode]['selection'])) {
      $variable_name = 'panelizer_' . $this->entity_type . ':' . $new_bundle . ':' . $panelizer->view_mode . '_selection';
      $old_value = variable_get($variable_name, NULL);
      $new_value = $form_state['values']['panelizer']['view modes'][$panelizer->view_mode]['selection'];

      // Save the variable.
      variable_set($variable_name, $new_value);

      // Cleanup.
      // Additional cleanup if the default display was changed.
      if (!is_null($old_value) && $old_value != $new_value) {

        // The user specifically requested that existing entities are to be
        // updated to the new display.
        if (!empty($form_state['values']['panelizer']['view modes'][$panelizer->view_mode]['default revert'])) {
          $updated_count = db_update('panelizer_entity')
            ->fields(array(
            'name' => $new_value,
          ))
            ->condition('name', $old_value)
            ->execute();
          drupal_set_message(t('@count @entity records were updated to the new Panelizer display for the @mode view mode.', array(
            '@count' => $updated_count,
            '@entity' => $this->entity_type,
            '@mode' => $panelizer->view_mode,
          )));

          // If EntityCache is enabled, clear all records of this type. This
          // is a little heavy-handed, but I don't believe there's an easy way
          // to clear only entities of certain types without querying for them
          // first, which could trigger an execution timeout.
          if (module_exists('entitycache')) {
            cache_clear_all('*', 'cache_entity_' . $this->entity_type, TRUE);
          }
        }
      }
    }
  }

  // Remove some settings that shouldn't be saved with the others.
  if (!empty($form_state['values']['panelizer']['view modes'])) {
    foreach ($form_state['values']['panelizer']['view modes'] as $view_mode => $settings) {
      unset($form_state['values']['panelizer']['view modes'][$view_mode]['selection']);
      unset($form_state['values']['panelizer']['view modes'][$view_mode]['default revert']);
    }
  }
  variable_set('panelizer_defaults_' . $this->entity_type . '_' . $new_bundle, $form_state['values']['panelizer']);

  // Verify the necessary Page Manager prerequisites are ready.
  if (!empty($form_state['values']['panelizer']['status']) && !empty($form_state['values']['panelizer']['view modes']['page_manager']['status']) && $this
    ->is_page_manager_enabled()) {
    $this
      ->check_page_manager_status();
  }

  // Unset this so that the type save forms don't try to save it to variables.
  unset($form_state['values']['panelizer']);
}