You are here

thunder_riddle.install in Thunder 8.5

Thunder Riddle integration install hooks.

File

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

/**
 * @file
 * Thunder Riddle integration install hooks.
 */
use Drupal\user\Entity\Role;

/**
 * Implements hook_install().
 *
 * Granting permissions for "editor" and "seo" user roles for proper functioning
 * of Riddle entity browser.
 */
function thunder_riddle_install() {
  $logger = \Drupal::logger('thunder_riddle');

  /** @var \Drupal\user\RoleInterface[] $roles */
  $roles = Role::loadMultiple([
    'editor',
    'seo',
  ]);
  foreach ($roles as $role) {
    $role
      ->grantPermission('access riddle_browser entity browser pages');
    if ($role
      ->save()) {
      $logger
        ->info(t('Permissions to access riddle entity browser granted for "@user_role" user role.', [
        '@user_role' => $role
          ->id(),
      ]));
    }
    else {
      $logger
        ->warning(t('Unable to grant access for riddle entity browser for "@user_role" user role.', [
        '@user_role' => $role
          ->id(),
      ]));
    }
  }

  /** @var \Drupal\field\Entity\FieldConfig $field */
  $field = \Drupal::entityTypeManager()
    ->getStorage('field_config')
    ->load('node.article.field_paragraphs');
  if ($field) {
    $settings = $field
      ->getSetting('handler_settings');
    if (!isset($settings['negate']) || !$settings['negate']) {
      $settings['target_bundles']['riddle'] = 'riddle';
      $settings['target_bundles_drag_drop']['riddle'] = [
        'enabled' => TRUE,
        'weight' => 10,
      ];
      $field
        ->setSetting('handler_settings', $settings);
      $field
        ->save();
    }
  }
}

/**
 * Add dependency on thunder_riddle where necessary.
 */
function thunder_riddle_update_8001() {

  /** @var \Drupal\Core\Config\ConfigManager $config_manager */
  $config_manager = \Drupal::service('config.manager');
  $config_entities = [
    'core.entity_form_display.media.riddle.default',
    'core.entity_form_display.paragraph.riddle.default',
    'core.entity_view_display.media.riddle.default',
    'core.entity_view_display.media.riddle.thumbnail',
    'core.entity_view_display.paragraph.riddle.default',
    'core.entity_view_display.paragraph.riddle.preview',
    'entity_browser.browser.riddle_browser',
    'field.field.paragraph.riddle.field_riddle',
    'field.storage.media.field_riddle_id',
    'field.storage.paragraph.field_riddle',
    'media.type.riddle',
    'paragraphs.paragraphs_type.riddle',
  ];
  $new_dependency = [
    'enforced' => [
      'module' => [
        'thunder_riddle',
      ],
    ],
  ];
  foreach ($config_entities as $name) {

    /** @var \Drupal\Core\Config\Entity\ConfigEntityBase $entity */
    $entity = $config_manager
      ->loadConfigEntityByName($name);

    // It's possible a user might have deleted one of the provided entities.
    if ($entity) {
      $dependencies = array_merge($entity
        ->get('dependencies'), $new_dependency);
      $entity
        ->set('dependencies', $dependencies)
        ->trustData()
        ->save();
    }
  }
}

Functions

Namesort descending Description
thunder_riddle_install Implements hook_install().
thunder_riddle_update_8001 Add dependency on thunder_riddle where necessary.