You are here

thunder_liveblog.install in Thunder 8.2

The install file.

File

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

/**
 * @file
 * The install file.
 */
use Drupal\field\Entity\FieldConfig;
use Drupal\user\Entity\Role;

/**
 * Implements hook_install().
 *
 * Configures form and view display of the field_embed_media.
 */
function thunder_liveblog_install() {

  // Add permissions to thunder users.
  $liveblog_permissions = [
    'add liveblog_post entity',
    'create liveblog content',
    'delete any liveblog content',
    'delete liveblog revisions',
    'delete liveblog_post entity',
    'delete own liveblog content',
    'edit any liveblog content',
    'edit liveblog_post entity',
    'edit own liveblog content',
    'revert liveblog revisions',
    'view liveblog revisions',
  ];
  foreach ([
    'editor',
    'seo',
  ] as $role) {
    if ($role = Role::load($role)) {
      foreach ($liveblog_permissions as $permission) {
        $role
          ->grantPermission($permission);
      }
      $role
        ->save();
    }
  }
}

/**
 * Delete liveblog provided config, we have our own.
 */
function thunder_liveblog_module_preinstall($module) {
  if ('thunder_liveblog' == $module) {
    $entity_query = \Drupal::entityQuery('node');
    $results = $entity_query
      ->condition('type', 'liveblog')
      ->exists('body')
      ->count()
      ->execute();
    if (($field = FieldConfig::load('node.liveblog.body')) && !$results) {
      $field
        ->delete();
      field_purge_batch(10);
    }
    $configs = [
      'core.entity_form_display.node.liveblog.default',
      'core.entity_form_display.liveblog_post.liveblog_post.default',
      'core.entity_view_display.liveblog_post.liveblog_post.default',
      'core.entity_view_display.node.liveblog.default',
    ];
    $configFactory = \Drupal::configFactory();
    foreach ($configs as $config) {
      $configFactory
        ->getEditable($config)
        ->delete();
    }
  }
}

/**
 * Adds image to the list of available paragraphs.
 */
function thunder_liveblog_update_8101() {

  /** @var \Drupal\update_helper\Updater $updater */
  $updater = \Drupal::service('update_helper.updater');
  $updateLogger = $updater
    ->logger();

  /** @var \Drupal\field\Entity\FieldConfig $field */
  $field = FieldConfig::load('liveblog_post.liveblog_post.field_embed_media');
  if ($field) {
    $settings = $field
      ->getSetting('handler_settings');
    $settings['target_bundles']['image'] = 'image';
    $settings['target_bundles_drag_drop']['image'] = [
      'enabled' => TRUE,
      'weight' => -17,
    ];
    $field
      ->setSetting('handler_settings', $settings);
    try {
      $field
        ->save();
      $updateLogger
        ->info(t('Image paragraph added to the liveblog post field "embed_media".'));
    } catch (Exception $e) {
      $updateLogger
        ->error(t('There was an error, while saving the liveblog post field "embed_media", please try to add the image paragraph manually.'));
    }
  }
  else {
    $updateLogger
      ->warning(t('Liveblog post field "embed_media" was not found. Please try to add the image paragraph manually.'));
  }

  // Output logged result of update hook execution.
  return $updateLogger
    ->output();
}

/**
 * Provide nicer liveblog form display.
 */
function thunder_liveblog_update_8102() {

  /** @var \Drupal\update_helper\Updater $updater */
  $updater = \Drupal::service('update_helper.updater');
  $updateLogger = $updater
    ->logger();
  if ($updater
    ->executeUpdate('thunder_liveblog', 'thunder_liveblog__update_8102')) {
    $updateLogger
      ->info(t('New form display successfully installed.'));
  }
  else {
    $updateLogger
      ->warning(t('There was an error during the update of the liveblog form display.'));
  }

  // Output logged messages to related channel of update execution.
  return $updateLogger
    ->output();
}

/**
 * Switch to new experimental paragraphs widget and paragraphs features.
 */
function thunder_liveblog_update_8103() {

  /** @var \Drupal\update_helper\Updater $updater */
  $updater = \Drupal::service('update_helper.updater');

  // Update should be marked as successful only if all steps are successful.
  $updater
    ->executeUpdate('thunder_liveblog', 'thunder_liveblog__update_8103');

  // Output logged messages to related chanel of update execution.
  return $updater
    ->logger()
    ->output();
}

Functions

Namesort descending Description
thunder_liveblog_install Implements hook_install().
thunder_liveblog_module_preinstall Delete liveblog provided config, we have our own.
thunder_liveblog_update_8101 Adds image to the list of available paragraphs.
thunder_liveblog_update_8102 Provide nicer liveblog form display.
thunder_liveblog_update_8103 Switch to new experimental paragraphs widget and paragraphs features.