You are here

thunder_paragraphs.install in Thunder 8.2

Contains update hooks for Thunder Paragraphs module.

File

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

/**
 * @file
 * Contains update hooks for Thunder Paragraphs module.
 */
use Drupal\Component\Utility\DiffArray;
use Drupal\Component\Utility\NestedArray;

/**
 * Update hook dependencies.
 *
 * 1. Dependency on Thunder Updater module.
 *
 * @return mixed
 *   Returns list of update hook dependencies.
 */
function thunder_paragraphs_update_dependencies() {
  $installThunderUpdaterHook = [
    'thunder' => 8103,
  ];
  $dependencies['thunder_paragraphs'] = [
    8001 => $installThunderUpdaterHook,
  ];
  return $dependencies;
}

/**
 * Change gallery paragraph to use simple inline form widget.
 */
function thunder_paragraphs_update_8001() {

  // List of expected configurations, to ensure it's provided by our configs.
  $expectedConfigs['field.field.paragraph.gallery.field_media']['required'] = FALSE;
  $expectedConfigs['core.entity_form_display.paragraph.gallery.default']['content']['field_media']['type'] = 'inline_entity_form_complex';
  $expectedConfigs['core.entity_form_display.paragraph.gallery.default']['content']['field_media']['settings']['allow_existing'] = TRUE;
  $expectedConfigs['core.entity_form_display.paragraph.gallery.default']['content']['field_media']['settings']['allow_new'] = TRUE;
  $expectedConfigs['core.entity_form_display.paragraph.gallery.default']['content']['field_media']['settings']['match_operator'] = 'CONTAINS';
  $expectedConfigs['core.entity_form_display.paragraph.gallery.default']['content']['field_media']['third_party_settings']['entity_browser_entity_form']['entity_browser_id'] = 'gallery_browser';

  // Config for Gallery paragraph field and field from display.
  $newConfigs['field.field.paragraph.gallery.field_media']['required'] = TRUE;
  $newConfigs['core.entity_form_display.paragraph.gallery.default']['content']['field_media']['type'] = 'inline_entity_form_simple';
  $newConfigs['core.entity_form_display.paragraph.gallery.default']['content']['field_media']['settings']['form_mode'] = 'default';

  // Configurations that will be overwritten without nested merging.
  $forcedConfigs['core.entity_form_display.paragraph.gallery.default'] = [
    [
      'key_path' => [
        'content',
        'field_media',
        'third_party_settings',
      ],
      'value' => [],
    ],
  ];

  // Configurations that will be removed.
  $obsoleteConfigs['core.entity_form_display.paragraph.gallery.default'] = [
    [
      'content',
      'field_media',
      'settings',
      'allow_existing',
    ],
    [
      'content',
      'field_media',
      'settings',
      'allow_new',
    ],
    [
      'content',
      'field_media',
      'settings',
      'match_operator',
    ],
  ];

  /** @var \Drupal\update_helper\UpdateLogger $updateLogger */
  $updateLogger = \Drupal::service('update_helper.logger');

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $configFactory */
  $configFactory = \Drupal::service('config.factory');
  foreach ($newConfigs as $configName => $newConfig) {

    /** @var \Drupal\Core\Config\Config $paragraphConfig */
    $config = $configFactory
      ->getEditable($configName);
    $configData = $config
      ->get();

    // Check that configuration exists before executing update.
    if (empty($configData) || DiffArray::diffAssocRecursive($expectedConfigs[$configName], $configData)) {
      $updateLogger
        ->warning(t('Unable to update configuration for "@configName".', [
        '@configName' => $configName,
      ]));
    }
    else {
      $mergedConfig = NestedArray::mergeDeep($configData, $newConfig);

      // Remove obsolete configurations.
      if (isset($obsoleteConfigs[$configName])) {
        foreach ($obsoleteConfigs[$configName] as $keyPath) {
          NestedArray::unsetValue($mergedConfig, $keyPath);
        }
      }

      // Set exact values for nested arrays.
      if (isset($forcedConfigs[$configName])) {
        foreach ($forcedConfigs[$configName] as $forcedValue) {
          NestedArray::unsetValue($mergedConfig, $forcedValue['key_path'], $forcedValue['value']);
        }
      }
      $config
        ->setData($mergedConfig);
      $config
        ->save();
    }
  }
  $updateLogger
    ->info('Gallery paragraph is updated.');
  return $updateLogger
    ->output();
}

/**
 * Change media paragraph to use inline_entity_form.
 */
function thunder_paragraphs_update_8101() {

  /** @var \Drupal\update_helper\Updater $updater */
  $updater = \Drupal::service('update_helper.updater');
  $updater
    ->executeUpdate('thunder', 'v1_1__media_paragraph');
  return $updater
    ->logger()
    ->output();
}

/**
 * Enable replace button for media entities.
 */
function thunder_paragraphs_update_8102() {

  /** @var \Drupal\update_helper\Updater $updater */
  $updater = \Drupal::service('update_helper.updater');
  $updater
    ->executeUpdate('thunder', 'thunder_paragraphs_update_8102');

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

Functions

Namesort descending Description
thunder_paragraphs_update_8001 Change gallery paragraph to use simple inline form widget.
thunder_paragraphs_update_8101 Change media paragraph to use inline_entity_form.
thunder_paragraphs_update_8102 Enable replace button for media entities.
thunder_paragraphs_update_dependencies Update hook dependencies.