You are here

function thunder_update_8109 in Thunder 8.2

Change paragraphs add mode to use the modal.

File

./thunder.install, line 567
Install, update and uninstall functions for the thunder installation profile.

Code

function thunder_update_8109() {

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

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::configFactory();

  // List of configurations that should be checked for existence.
  $expectedConfig['content']['field_paragraphs']['settings']['add_mode'] = 'dropdown';

  // New configuration that should be applied.
  $newConfig['content']['field_paragraphs']['settings']['add_mode'] = 'modal';
  $successfulUpdate = TRUE;
  foreach ([
    'node.article',
    'taxonomy_term.channel',
    'taxonomy_term.tags',
  ] as $item) {
    $config = $config_factory
      ->getEditable('core.entity_form_display.' . $item . '.default');
    $config_data = $config
      ->get();

    // Check that configuration exists before executing update.
    if (empty($config_data)) {
      $successfulUpdate = FALSE;
      $updateLogger
        ->warning(t('Unable to adjust form display for @item default', [
        '@item' => $item,
      ]));
      continue;
    }

    // Check if configuration is already in new state.
    $merged_data = NestedArray::mergeDeep($expectedConfig, $newConfig);
    if (empty(DiffArray::diffAssocRecursive($merged_data, $config_data))) {
      $updateLogger
        ->info(t('Default form display for @item has already new configuration.', [
        '@item' => $item,
      ]));
      continue;
    }
    if (!empty($expectedConfig) && DiffArray::diffAssocRecursive($expectedConfig, $config_data)) {
      $successfulUpdate = FALSE;
      $updateLogger
        ->warning(t('Unable to adjust form display for @item default', [
        '@item' => $item,
      ]));
      continue;
    }
    $config
      ->setData(NestedArray::mergeDeep($config_data, $newConfig));
    $config
      ->save();
    $updateLogger
      ->info(t('Adjusted form display for @item default', [
      '@item' => $item,
    ]));
  }
  if (!$successfulUpdate) {
    $updateLogger
      ->warning(t('Unable to adjust paragraphs add mode.'));
  }
  _thunder_mark_update_checklist('v1_1__paragraphs_modal', $successfulUpdate, $updateLogger);
  return $updateLogger
    ->output();
}