You are here

function date_recur_modular_update_8201 in Recurring Date Field Modular Widgets 3.x

Same name and namespace in other branches
  1. 8 date_recur_modular.install \date_recur_modular_update_8201()
  2. 2.x date_recur_modular.install \date_recur_modular_update_8201()

Updates existing form displays with default interpreter.

File

./date_recur_modular.install, line 16

Code

function date_recur_modular_update_8201(&$sandbox) : void {
  $interpreterId = 'default_interpreter';
  if (!DateRecurInterpreter::load($interpreterId)) {

    // If default interpreter doesnt exist then skip this update.
    return;
  }

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface[] $displays */
  $displays = EntityFormDisplay::loadMultiple();
  foreach ($displays as $display) {
    $updated = FALSE;
    $components = $display
      ->getComponents();
    foreach ($components as $component => $options) {
      $type = $options['type'] ?? NULL;
      if ($type === 'date_recur_modular_sierra' && ($options['settings']['interpreter'] ?? NULL) === NULL) {

        // Adding this will automatically add dependency.
        $options['settings']['interpreter'] = $interpreterId;
        $updated = TRUE;
        $display
          ->setComponent($component, $options);
      }
    }
    if ($updated) {
      $display
        ->save();
    }
  }
}