You are here

public function PanelizerEntityDefault::settings_form_submit in Panelizer 7.3

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

Submit entity specific settings on the Panelizer settings form.

Overrides PanelizerEntityInterface::settings_form_submit

File

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

Class

PanelizerEntityDefault
Base class for the Panelizer Entity plugin.

Code

public function settings_form_submit(&$form, &$form_state) {
  if (empty($form_state['values']['entities'][$this->entity_type])) {
    return;
  }
  foreach ($form_state['values']['entities'][$this->entity_type] as $bundle => $values) {

    // Rewrite our settings because they're not quite in the right format in
    // the form.
    $settings = array(
      'status' => $values[0]['status'],
      'view modes' => array(),
    );
    if (!empty($values[0]['status'])) {

      // This field is optional so should not always be applied.
      if (isset($values[0]['help']) && !empty($values[0]['help'])) {
        $settings['help'] = $values[0]['help'];
      }
      foreach ($values as $view_mode => $config) {
        if (!empty($view_mode) && !empty($config)) {

          // Fix the configuration.
          // Make sure each setting is disabled if the view mode is disabled.
          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.
          if (!empty($config['selection'])) {
            $variable_name = 'panelizer_' . $this->entity_type . ':' . $bundle . ':' . $view_mode . '_selection';
            $old_value = variable_get($variable_name, NULL);
            $new_value = $config['selection'];
            variable_set($variable_name, $config['selection']);

            // 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($config['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' => $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);
                }
              }
            }
          }

          // Don't save some settings with the rest of the settings bundle.
          unset($config['selection']);
          unset($config['default revert']);
          $settings['view modes'][$view_mode] = $config;
        }
      }
    }
    variable_set('panelizer_defaults_' . $this->entity_type . '_' . $bundle, $settings);
  }

  // @todo if we enable caching of the plugins, which we should, this
  // needs to clear that cache so they get reloaded.
}