You are here

lightning_test.install in Lightning Core 8

Same filename and directory in other branches
  1. 8.2 modules/lightning_test/lightning_test.install

Contains installation routines for Lightning Test.

File

modules/lightning_test/lightning_test.install
View source
<?php

/**
 * @file
 * Contains installation routines for Lightning Test.
 */
use Drupal\Core\Form\FormState;
use Drupal\field\Entity\FieldConfig;
use Drupal\entity_browser\Element\EntityBrowserElement;

/**
 * Implements hook_install().
 */
function lightning_test_install() {
  entity_get_form_display('node', 'page', 'default')
    ->setComponent('field_multi_image', [
    'type' => 'entity_browser_file',
    'settings' => [
      'entity_browser' => 'image_browser',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'view_mode' => 'default',
      'preview_image_style' => 'thumbnail',
      'open' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
    ],
    'region' => 'content',
  ])
    ->setComponent('field_hero_image', [
    'type' => 'entity_browser_file',
    'settings' => [
      'entity_browser' => 'image_browser',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'view_mode' => 'default',
      'preview_image_style' => 'thumbnail',
      'open' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
    ],
    'region' => 'content',
  ])
    ->setComponent('field_lightweight_image', [
    'type' => 'entity_browser_file',
    'settings' => [
      'entity_browser' => 'image_browser',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'view_mode' => 'default',
      'preview_image_style' => 'thumbnail',
      'open' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
    ],
    'region' => 'content',
  ])
    ->setComponent('field_unlimited_images', [
    'type' => 'entity_browser_file',
    'settings' => [
      'entity_browser' => 'image_browser',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'view_mode' => 'default',
      'preview_image_style' => 'thumbnail',
      'open' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
    ],
    'region' => 'content',
  ])
    ->setComponent('field_media_reference', [
    'type' => 'entity_browser_entity_reference',
    'settings' => [
      'entity_browser' => 'media_browser',
      'field_widget_display' => 'rendered_entity',
      'field_widget_edit' => TRUE,
      'field_widget_remove' => TRUE,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
      'field_widget_display_settings' => [
        'view_mode' => 'embedded',
      ],
      'open' => TRUE,
    ],
    'region' => 'content',
  ])
    ->save();
  $display = entity_get_form_display('media', 'video', 'default');

  // Add field_image to the display and save it; lightning_media_image will
  // default it to the image browser widget.
  $display
    ->setComponent('field_image', [
    'type' => 'image_image',
  ])
    ->save();

  // Then switch it to a standard image widget.
  $display
    ->setComponent('field_image', [
    'type' => 'image_image',
    'weight' => 4,
    'settings' => [
      'preview_image_style' => 'thumbnail',
      'progress_indicator' => 'throbber',
    ],
    'region' => 'content',
  ])
    ->save();
}

/**
 * Implements hook_uninstall().
 */
function lightning_test_uninstall() {
  $state = \Drupal::state();

  // Delete all content entities created while the module was installed.
  $delete = $state
    ->get('_delete', []);
  foreach ($delete as $entity_type => $entities) {
    $storage = \Drupal::entityTypeManager()
      ->getStorage($entity_type);
    $entities = $storage
      ->loadMultiple($entities);
    $storage
      ->delete($entities);
  }
  $state
    ->delete('_delete');

  // Delete all fields created while the module was installed. The most stable
  // way to do this is to execute the entity form which deletes a field.
  $fields = $state
    ->get('_fields', []);
  if ($fields) {
    $form = \Drupal::entityTypeManager()
      ->getFormObject('field_config', 'delete');
    $fields = FieldConfig::loadMultiple($fields);
    foreach ($fields as $field) {
      $form_state = new FormState();
      \Drupal::formBuilder()
        ->submitForm($form
        ->setEntity($field), $form_state);
    }
  }
  $state
    ->delete('_fields');
}

Functions