You are here

function entity_view_mode_save in Entity view modes 7

Save a custom entity view mode.

1 call to entity_view_mode_save()
entity_view_mode_edit_form_submit in ./entity_view_mode.admin.inc

File

./entity_view_mode.module, line 139

Code

function entity_view_mode_save($entity_type, $view_mode) {
  $existing_view_mode = !empty($view_mode['old_machine_name']) ? $view_mode['old_machine_name'] : $view_mode['machine_name'];
  $view_mode_name = $view_mode['machine_name'];

  // Load the original, unchanged view mode, if it exists.
  if ($original = entity_view_mode_load($entity_type, $existing_view_mode)) {
    $view_mode['original'] = $original;
    unset($view_mode['old_machine_name']);
  }

  // The key value for custom settings needs to be converted from
  // 'custom_settings' to 'custom settings' before saving.
  if (isset($view_mode['custom_settings'])) {
    $view_mode['custom settings'] = $view_mode['custom_settings'];
    unset($view_mode['custom_settings']);
  }

  // Determine if we will be inserting a new view mode.
  if (!isset($view_mode['is_new'])) {
    $view_mode['is_new'] = empty($view_mode['original']);
  }

  // Let modules modify the view mode before it is saved.
  module_invoke_all('entity_view_mode_presave', $view_mode, $entity_type);

  // Save the view mode.
  $view_modes = variable_get('entity_view_modes', array());
  unset($view_modes[$entity_type][$existing_view_mode]);
  $view_modes[$entity_type][$view_mode_name] = array_intersect_key($view_mode, drupal_map_assoc(array(
    'label',
    'custom settings',
  )));
  $view_modes[$entity_type][$view_mode_name] += array(
    'custom settings' => TRUE,
  );
  variable_set('entity_view_modes', $view_modes);

  // Allow modules to respond after the view mode is saved.
  if ($view_mode['is_new']) {
    module_invoke_all('entity_view_mode_insert', $view_mode, $entity_type);
  }
  else {

    // If the view mode's machine name has changed, invoke a separate hook
    // before running the update hook.
    if ($view_mode_name != $existing_view_mode) {
      module_invoke_all('entity_view_mode_rename', $entity_type, $existing_view_mode, $view_mode_name);
    }
    module_invoke_all('entity_view_mode_update', $view_mode, $entity_type);
  }

  // Clear internal properties.
  unset($view_mode['original']);
  unset($view_mode['is_new']);

  // Clear the static entity info cache and rebuild the menu.
  entity_info_cache_clear();
  variable_set('menu_rebuild_needed', TRUE);
}