You are here

function prod_check_synchronize_prod_checks in Production check & Production monitor 8

Helper function to synchronize prod check plugins and entities

1 call to prod_check_synchronize_prod_checks()
prod_check_cache_flush in ./prod_check.module
Implements hook_cache_flush().

File

./prod_check.module, line 113
Module file for the prod check module

Code

function prod_check_synchronize_prod_checks() {

  // This isn't a nice way but at the moment it's the only way I can think of to keep the one on one relation between
  // prod check entities and plugins intact. As the amount of checks will stay limited this isn't a performance issue.
  $query = \Drupal::entityQuery('prod_check')
    ->condition('status', 1);
  $existing_checks = $query
    ->execute();
  $checkManager = \Drupal::service('plugin.manager.prod_check');
  $checks = $checkManager
    ->getDefinitions();
  foreach ($checks as $plugin_id => $check) {
    $plugin = $checkManager
      ->createInstance($plugin_id, $check);
    $id = !empty($plugin
      ->getDerivativeId()) ? $plugin
      ->getBaseId() . '_' . $plugin
      ->getDerivativeId() : $plugin_id;
    if (!isset($existing_checks[$id])) {
      $values = [
        'id' => $id,
        'label' => (string) $plugin
          ->title(),
      ];

      /** @var ProdCheckEntity $new_check */
      $new_check = \Drupal::entityTypeManager()
        ->getStorage('prod_check')
        ->create($values);
      $new_check
        ->setPlugin($plugin_id);
      $new_check
        ->save();
    }
  }
}