You are here

function domain_source_presave_generate in Domain Access 8

Handles presave operations for devel generate.

1 call to domain_source_presave_generate()
domain_source_node_presave in domain_source/domain_source.module
Implements hook_ENTITY_TYPE_presave().

File

domain_source/domain_source.module, line 267
Domain-based path rewrites for content.

Code

function domain_source_presave_generate(EntityInterface $entity) {

  // Handle devel module settings.
  $exists = \Drupal::moduleHandler()
    ->moduleExists('devel_generate');
  $values = [];
  $selections = [];
  if ($exists && isset($entity->devel_generate)) {

    // If set by the form.
    if (isset($entity->devel_generate['domain_access'])) {
      $selection = array_filter($entity->devel_generate['domain_access']);
      if (isset($selection['random-selection'])) {
        $domains = \Drupal::entityTypeManager()
          ->getStorage('domain')
          ->loadMultiple();
        $selections = array_rand($domains, ceil(rand(1, count($domains))));
      }
      else {
        $selections = array_keys($selection);
      }
    }
    if (isset($entity->devel_generate['domain_source'])) {
      $selection = $entity->devel_generate['domain_source'];
      if ($selection == '_derive') {
        if (!empty($selections)) {
          $values[DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD] = current($selections);
        }
        else {
          $values[DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD] = NULL;
        }
      }
      foreach ($values as $name => $value) {
        $entity
          ->set($name, $value);
      }
    }
  }
}