You are here

public function BootstrapLayoutsManager::update in Bootstrap Layouts 8.5

Same name and namespace in other branches
  1. 8.4 src/BootstrapLayoutsManager.php \Drupal\bootstrap_layouts\BootstrapLayoutsManager::update()

Runs update(s) for a specific schema version.

Parameters

int $schema: The schema version to update.

bool $display_messages: Flag determining whether a message will be displayed indicating whether the layout was processed successfully or not.

File

src/BootstrapLayoutsManager.php, line 242

Class

BootstrapLayoutsManager
Class BootstrapLayoutsManager

Namespace

Drupal\bootstrap_layouts

Code

public function update($schema, $display_messages = TRUE) {
  $handlers = $this
    ->getHandlers();
  $data = [];
  foreach ($this->updateManager
    ->getUpdates($schema) as $update) {

    // See if there's an adjoining YML file with the update plugin.
    $r = new \ReflectionClass($update);
    $data_paths = [
      dirname($r
        ->getFileName()),
      $update
        ->getPath(),
    ];

    // Merge in any update data.
    foreach ($data_paths as $path) {
      $file = "{$path}/bootstrap_layouts.update.{$schema}.yml";
      if (file_exists($file) && ($yaml = Yaml::decode(file_get_contents($file)))) {
        $data = NestedArray::mergeDeep($data, $yaml);
      }
    }

    // Perform the update.
    $update
      ->update($this, $data, $display_messages);

    // Process any existing layouts after the update.
    foreach ($handlers as $handler_id => $handler) {
      foreach ($handler
        ->loadInstances() as $storage_id => $layout) {
        $update
          ->processExistingLayout($layout, $data, $display_messages);

        // Determine if the layout has changed and then save it.
        if ($layout
          ->hasChanged()) {
          try {
            $handler
              ->saveInstance($storage_id, $layout);
            if ($display_messages) {
              $message = $this
                ->t('Successfully updated the existing Bootstrap layout found in "@id".', [
                '@id' => $storage_id,
              ]);
            }
          } catch (\Exception $exception) {
            $message = $this
              ->t('Unable to update the existing Bootstrap layout found in "@id":', [
              '@id' => $storage_id,
            ]);
          }
          if (isset($message)) {
            $error = isset($exception) ? $exception
              ->getMessage() : FALSE;
            $type = $error ? 'error' : 'status';
            if (\Drupal::hasService('messenger') && ($messenger = \Drupal::messenger())) {
              $messenger
                ->addMessage($message, $type);
              if ($error) {
                $messenger
                  ->addError($error);
              }
            }
            else {
              drupal_set_message($message, $type);
              if ($error) {
                drupal_set_message($error, 'error');
              }
            }
          }
        }
      }
    }
  }
}