You are here

media_directories_editor.install in Media Directories 8

Same filename and directory in other branches
  1. 2.x modules/media_directories_editor/media_directories_editor.install

Install and database hook implementations.

File

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

/**
 * @file
 * Install and database hook implementations.
 */
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\media\Entity\MediaType;
use Drupal\Core\Entity\Entity\EntityViewMode;

/**
 * Implements hook_install().
 */
function media_directories_editor_install() {

  // Do not add button config, if we aren't installing from configuration.
  if (!\Drupal::isConfigSyncing()) {
    $config = \Drupal::configFactory()
      ->getEditable('embed.button.media_directories');

    // Add the standard media types for the embed button, if available.
    $media_types_to_add = [
      'image',
      'remote_video',
    ];
    $media_types = MediaType::loadMultiple();
    foreach ($media_types_to_add as $delta => $type) {
      if (!isset($media_types[$type])) {
        unset($media_types_to_add[$delta]);
      }
    }
    if (count($media_types_to_add) > 0) {
      $config
        ->set('type_settings.bundles', $media_types_to_add);
    }

    // Add the standard media entity view mode, if available.
    $media_full_view_mode = EntityViewMode::load('media.full');
    if ($media_full_view_mode) {
      $display_plugins = $config
        ->get('type_settings.display_plugins');
      $display_plugins[] = 'view_mode:' . $media_full_view_mode
        ->id();
      $config
        ->set('type_settings.display_plugins', $display_plugins);
    }
    $config
      ->save();
  }
}

/**
 * Implements hook_uninstall().
 */
function media_directories_editor_uninstall() {

  // Remove the entity browsers.
  \Drupal::configFactory()
    ->getEditable('entity_browser.browser.media_directories_editor_browser')
    ->delete();

  // Remove the embed button.
  \Drupal::configFactory()
    ->getEditable('embed.button.media_directories')
    ->delete();
}

/**
 * Update the entity browser's widget id.
 */
function media_directories_editor_update_8001() {

  // Update the entity browser.
  $config = \Drupal::configFactory()
    ->getEditable('entity_browser.browser.media_directories_editor_browser');
  $config
    ->set('widgets.5bc2a17c-b080-4a3b-867c-7953ae9b55c4.id', 'media_directories_browser_widget');
  $config
    ->save();
}

/**
 * Rename the entity browsers labels.
 */
function media_directories_editor_update_8002() {
  $config = \Drupal::configFactory()
    ->getEditable('entity_browser.browser.media_directories_editor_browser');
  $config
    ->set('label', 'Media Directories: Editor widget');
  $config
    ->save();
  $language_manager = \Drupal::languageManager();
  if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
    foreach ($language_manager
      ->getLanguages() as $language) {
      $config_translation = $language_manager
        ->getLanguageConfigOverride($language
        ->getId(), 'entity_browser.browser.media_directories_editor_browser');
      $config_translation
        ->set('label', 'Media Directories: Editor widget');
      $config_translation
        ->save();
    }
  }
}

Functions

Namesort descending Description
media_directories_editor_install Implements hook_install().
media_directories_editor_uninstall Implements hook_uninstall().
media_directories_editor_update_8001 Update the entity browser's widget id.
media_directories_editor_update_8002 Rename the entity browsers labels.