/** * On workflow transition. * * @param \Drupal\state_machine\Event\StateChangeEvent $event * The state change event. * * @throws \Drupal\message_notify\Exception\MessageNotifyException */ public function onChangeToValidated(StateChangeEvent $event) { $entity = $event->getEntity(); $bundle = $entity->bundle(); if ($bundle != 'solution') { return; } $message_template = MessageTemplate::load('workflow_transition'); $storage = $this->entityTypeManager->getStorage('og_membership'); // Sent a message to all solution administrators. $membership_query = $storage->getQuery()->condition('state', 'active')->condition('entity_id', $entity->id()); $memberships_ids = $membership_query->execute(); $memberships = OgMembership::loadMultiple($memberships_ids); $memberships = array_filter($memberships, function ($membership) { return $membership->hasPermission('message notification on validate'); }); /** @var OgMembership $membership */ foreach ($memberships as $membership) { $uid = $membership->get('uid')->first()->getValue()['target_id']; // Create the actual message and save it to the db. $message = Message::create(['template' => $message_template->id(), 'uid' => $uid, 'field_message_content' => $entity->id()]); $message->save(); // Send the saved message as an e-mail. $this->messageNotifier->send($message, [], 'email'); } }
/** * Creates an Og membership to a group optionally assigning roles as well. * * @param \Drupal\Core\Session\AccountInterface $user * The user to be assigned as a group member. * @param \Drupal\Core\Entity\EntityInterface $entity * The organic group entity. * @param \Drupal\og\Entity\OgRole[] $roles * An array of OgRoles to be passed to the membership. * * @throws \Exception * Throws an exception when the user is anonymous or the entity is not a * group. */ protected function subscribeUserToGroup(AccountInterface $user, EntityInterface $entity, array $roles = []) { if (!Og::isGroup($entity->getEntityTypeId(), $entity->bundle())) { throw new \Exception("The {$entity->label()} is not a group."); } /** @var \Drupal\og\OgMembershipInterface $membership */ $membership = OgMembership::create(); $membership->setUser($user)->setGroup($entity); foreach ($roles as $role) { $membership->addRole($role); } $membership->save(); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var RdfInterface $collection */ $collection = Rdf::load($form_state->getValue('collection_id')); /** @var \Drupal\user\UserInterface $user */ $user = User::load($form_state->getValue('user_id')); $role_id = $collection->getEntityTypeId() . '-' . $collection->bundle() . '-' . OgRoleInterface::AUTHENTICATED; $membership = OgMembership::create(); $membership->setUser($user)->setGroup($collection)->setState(OgMembershipInterface::STATE_ACTIVE)->setRoles([OgRole::load($role_id)])->save(); drupal_set_message($this->t('You are now a member of %collection.', ['%collection' => $collection->getName()])); // @todo: This is a temporary workaround for the lack of og cache // contexts/tags. Remove this when Og provides proper cache context. // @see: https://webgate.ec.europa.eu/CITnet/jira/browse/ISAICP-2628 Cache::invalidateTags(['user.roles']); }