You are here

public function EventType::postSave in RNG - Events and Registrations 8

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides EntityBase::postSave

File

src/Entity/EventType.php, line 353

Class

EventType
Defines the event type entity.

Namespace

Drupal\rng\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);

  // Create mode for the entity type.
  $mode_id = $this->entity_type . '.rng_event';
  if (!EntityFormMode::load($mode_id)) {
    EntityFormMode::create([
      'id' => $mode_id,
      'targetEntityType' => $this->entity_type,
      'label' => 'Event Settings',
      'status' => TRUE,
    ])
      ->save();
  }
  if (!$update) {
    module_load_include('inc', 'rng', 'rng.field.defaults');
    foreach ($this->fields as $field) {
      rng_add_event_field_storage($field, $this->entity_type);
      rng_add_event_field_config($field, $this
        ->getEventEntityTypeId(), $this
        ->getEventBundle());
    }
  }
  $display = entity_get_form_display($this->entity_type, $this->bundle, 'rng_event');
  if ($display
    ->isNew()) {

    // EntityDisplayBase::init() adds default fields. Remove them.
    foreach (array_keys($display
      ->getComponents()) as $name) {
      if (!in_array($name, $this->fields)) {
        $display
          ->removeComponent($name);
      }
    }

    // Weight is the key.
    $field_weights = [
      EventManagerInterface::FIELD_STATUS,
      EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS,
      EventManagerInterface::FIELD_CAPACITY,
      EventManagerInterface::FIELD_EMAIL_REPLY_TO,
      EventManagerInterface::FIELD_REGISTRATION_TYPE,
      EventManagerInterface::FIELD_REGISTRATION_GROUPS,
      EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MINIMUM,
      EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MAXIMUM,
    ];
    module_load_include('inc', 'rng', 'rng.field.defaults');
    foreach ($this->fields as $name) {
      rng_add_event_form_display_defaults($display, $name);
      if (in_array($name, $field_weights)) {
        $component = $display
          ->getComponent($name);
        $component['weight'] = array_search($name, $field_weights);
        $display
          ->setComponent($name, $component);
      }
    }
    $display
      ->save();
  }

  // Rebuild routes and local tasks.
  \Drupal::service('router.builder')
    ->setRebuildNeeded();

  // Rebuild local actions https://github.com/dpi/rng/issues/18
  \Drupal::service('plugin.manager.menu.local_action')
    ->clearCachedDefinitions();
}