You are here

view_modes_display.module in View Modes Display 7

Same filename and directory in other branches
  1. 8.2 view_modes_display.module
  2. 8 view_modes_display.module

File

view_modes_display.module
View source
<?php

/**
 * Implements hook_menu().
 */
function view_modes_display_menu() {
  $items = array();
  $items['node/%node/preview'] = array(
    'title' => 'Preview display',
    'description' => 'Preview display of this content in the enabled view modes.',
    'page callback' => 'view_modes_display_preview_entity',
    'page arguments' => array(
      1,
      'node',
    ),
    'file' => 'view_modes_display.pages.inc',
    'access arguments' => array(
      'preview view modes',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['user/%user/preview'] = array(
    'title' => 'Preview display',
    'description' => 'Preview display of this user profile.',
    'page callback' => 'view_modes_display_preview_entity',
    'page arguments' => array(
      1,
      'user',
    ),
    'file' => 'view_modes_display.pages.inc',
    'access arguments' => array(
      'preview view modes',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  if (module_exists('taxonomy')) {
    $items['taxonomy/term/%taxonomy_term/preview'] = array(
      'title' => 'Preview display',
      'description' => 'Preview display of this term.',
      'page callback' => 'view_modes_display_preview_entity',
      'page arguments' => array(
        2,
        'taxonomy_term',
      ),
      'access arguments' => array(
        'preview view modes',
      ),
      'file' => 'view_modes_display.pages.inc',
      'type' => MENU_LOCAL_TASK,
      'weight' => 12,
    );
  }
  if (module_exists('profile2_page')) {
    foreach (profile2_get_types() as $type_name => $type) {
      if (!empty($type->data['use_page'])) {
        $path = profile2_page_get_base_path($type);
        $count = count(explode('/', $path));
        $items[$path . '/%profile2_by_uid/preview'] = array(
          'title' => 'Preview display',
          'description' => 'Preview display of this profile2.',
          'page callback' => 'view_modes_display_preview_entity',
          'page arguments' => array(
            $count,
            'profile2',
          ),
          'access callback' => 'profile2_access',
          'access arguments' => array(
            'edit',
            $count,
          ),
          'file' => 'view_modes_display.pages.inc',
          'type' => MENU_LOCAL_TASK,
          'weight' => 12,
        );
      }
    }
  }
  return $items;
}

/**
 * Implements hook_permission().
 */
function view_modes_display_permission() {
  return array(
    'preview view modes' => array(
      'title' => t('Peview entities in all available view modes'),
      'description' => t('Allows users to preview an entity in all available view modes on a page.'),
    ),
  );
}

/**
 * Implements hook_contextual_links_view_alter().
 */
function view_modes_display_contextual_links_view_alter(&$element, $items) {
  if (!empty($element['#element']['#entity_type']) && !empty($element['#element']['#bundle']) && !empty($element['#element']['#node']) && user_access('administer content types')) {
    $entity_type = $element['#element']['#entity_type'];
    $bundle = $element['#element']['#bundle'];
    $view_mode = isset($element['#element']['#view_mode']) ? $element['#element']['#view_mode'] : 'default';

    // Get the preview URI.
    $preview_path = 'node/' . $element['#element']['#node']->nid . '/preview';

    // Check view mode settings.
    $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
    $overridden = !empty($view_mode_settings[$view_mode]['custom_settings']) ? TRUE : FALSE;
    $element['#links']['preview-display'] = array(
      'title' => t('Preview display'),
      'href' => $preview_path,
      'query' => drupal_get_destination(),
    );
  }
}