✍️
Dev Patterns
  • Dev Patterns: Introduction
  • Drupal 8
    • Form Alter
      • data-twig-suggestion preprocess
      • Address field #after_build
    • Language
      • Language direction classes
      • Set twig variables for languages
      • Get the language direction (PHP)
    • Routes
      • Alter page title based on route
    • Preprocess Page
      • Preprocess users
      • Check if a node has body content and create a variable
    • Preprocess HTML
      • Check node ids and field values in preprocess_html
      • Add a body class based on route
    • General Theming
      • Preprocess Paragraph
      • Paragraph theme hook for a specific field value
      • Field theme hook suggestion based on the entity string
    • Twig
      • Twig include file
      • Twig loops
      • Loop index
    • Page alter
      • Page title theme hook
      • Page content type theme hook
      • Region alter
    • Users
      • Twig user variables
      • User view mode theme hook
    • Fields
      • Replace field name
      • Count number in a multi-value field array
    • Terminal
      • Query block ids for use with twig tweak
      • Check for available updates in terminal
  • Drupal 7
    • Create View modes programtically
    • Node form alter
    • Element Children
Powered by GitBook
On this page

Was this helpful?

  1. Drupal 7

Create View modes programtically

/**
 * Implements hook_entity_info_alter().
 *
 * Set up view modes as an array.
 */
function dev_patterns_entity_info_alter(&$entity_info) {
  // Node display modes.
  $entity_info['node']['view modes'] += [
    'person_tile' => [
      'label' => t('Person Tile'),
      'custom settings' => FALSE,
    ],
    '3up_card' => [
      'label' => t('3-Up Card'),
      'custom settings' => FALSE,
    ],
    'hero' => [
      'label' => t('Hero'),
      'custom settings' => FALSE,
    ],
  ];
}

Other view modes:

  // Field Collection display modes.
  $entity_info['field_collection_item']['view modes'] += []

  // Taxonomy Term display modes.
  $entity_info['taxonomy_term']['view modes'] += [

  // File display modes.
  $entity_info['file']['view modes'] += []

  // Bean display modes.
  $entity_info['bean']['view modes'] += []

  // Paragraphs Item view modes.
  $entity_info['paragraphs_item']['view modes'] += []
PreviousCheck for available updates in terminalNextNode form alter

Last updated 5 years ago

Was this helpful?