account = $account; $this->entityTypeManager = $entity_type_manager; $this->eventGenerator = $event_generator; $this->stomp = $stomp; $this->auth = $auth; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('current_user'), $container->get('entity_type.manager'), $container->get('islandora.eventgenerator'), $container->get('islandora.stomp'), $container->get('jwt.authentication.jwt') ); } /** * {@inheritdoc} */ public function execute($entity = NULL) { // Include a token for later authentication in the message. $token = $this->auth->generateToken(); if (empty($token)) { // JWT isn't properly configured. Log and notify user. \Drupal::logger('islandora')->error( t('Error getting JWT token for message. Check JWT Configuration.') ); drupal_set_message( t('Error getting JWT token for message. Check JWT Configuration.'), 'error' ); return; } // Generate event as stomp message. try { $user = $this->entityTypeManager->getStorage('user')->load($this->account->id()); $data = $this->generateData($entity); $message = new Message( $this->eventGenerator->generateEvent($entity, $user, $data), ['Authorization' => "Bearer $token"] ); } catch (\RuntimeException $e) { // Notify the user the event couldn't be generated and abort. \Drupal::logger('islandora')->error( t('Error generating event: @msg', ['@msg' => $e->getMessage()]) ); drupal_set_message( t('Error generating event: @msg', ['@msg' => $e->getMessage()]), 'error' ); return; } // Send the message. try { $this->stomp->begin(); $this->stomp->send($this->configuration['queue'], $message); $this->stomp->commit(); } catch (StompException $e) { // Log it. \Drupal::logger('islandora')->error( 'Error publishing message: @msg', ['@msg' => $e->getMessage()] ); // Notify user. drupal_set_message( t('Error publishing message: @msg', ['@msg' => $e->getMessage()] ), 'error' ); } } /** * Override this function to control what gets encoded as a json note. */ protected function generateData(EntityInterface $entity) { return $this->configuration; } /** * {@inheritdoc} */ public function defaultConfiguration() { return [ 'queue' => '', 'event' => 'Create', ]; } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['queue'] = [ '#type' => 'textfield', '#title' => t('Queue'), '#default_value' => $this->configuration['queue'], '#required' => TRUE, '#rows' => '8', '#description' => t('Name of queue to which event is published'), ]; $form['event'] = [ '#type' => 'select', '#title' => t('Event type'), '#default_value' => $this->configuration['event'], '#description' => t('Type of event to emit'), '#options' => [ 'Create' => t('Create'), 'Update' => t('Update'), 'Delete' => t('Delete'), 'Generate Derivative' => t('Generate Derivative'), ], ]; return $form; } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $this->configuration['queue'] = $form_state->getValue('queue'); $this->configuration['event'] = $form_state->getValue('event'); } /** * {@inheritdoc} */ public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { $result = AccessResult::allowed(); return $return_as_object ? $result : $result->isAllowed(); } }