1 entity.module entity_view_mode_save($entity_type, $view_mode)

Save a custom display mode.


string $entity_type: The entity type of the display mode to be saved, such as "node", "comment", "user", or "taxonomy_term".

array $view_mode: Fully loaded display mode, ready for saving.


core/modules/entity/entity.module, line 84
Entity API for handling entities like nodes or users.


function entity_view_mode_save($entity_type, $view_mode) {
  $view_mode_name = $view_mode['machine_name'];

  // Load the original, unchanged display mode, if it exists.
  if ($original = entity_view_mode_load($entity_type, $view_mode_name)) {
    $view_mode['original'] = $original;

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

  // Let modules modify the display mode before it is saved.
  $view_mode_invoke = module_invoke_all('entity_view_mode_presave', $view_mode, $entity_type);
  if (isset($view_mode_invoke)) {
    $view_mode = array_merge($view_mode, $view_mode_invoke);

  // Save the display mode.
  $view_modes = config_get('entity.view_modes', 'view_modes');
  $view_modes[$entity_type][$view_mode_name] = array('label' => $view_mode['label']);
  config_set('entity.view_modes', 'view_modes', $view_modes);

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

  // Clear internal properties.

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