/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); $collection = Rdf::load($form_state->getValue('collection_id')); // Only authenticated users can join a collection. /** @var \Drupal\user\UserInterface $user */ $user = User::load($form_state->getValue('user_id')); if ($user->isAnonymous()) { $form_state->setErrorByName('user', $this->t('<a href=":login">Log in</a> or <a href=":register">register</a> to change your group membership.', [':login' => Url::fromRoute('user.login'), ':register' => Url::fromRoute('user.register')])); } // Check if the user is already a member of the collection. if (Og::isMember($collection, $user)) { $form_state->setErrorByName('collection', $this->t('You already are a member of this collection.')); } }
/** * Access check for the LeaveCollectionConfirmForm. * * @param \Drupal\rdf_entity\RdfInterface $rdf_entity * The collection that is on the verge of losing a member. * * @return \Drupal\Core\Access\AccessResult * The access result object. */ public static function access(RdfInterface $rdf_entity) { /** @var \Drupal\Core\Session\AccountProxyInterface $account_proxy */ $account_proxy = \Drupal::service('current_user'); // Deny access if the user is not logged in. if ($account_proxy->isAnonymous()) { return AccessResult::forbidden(); } // Only allow access if the current user is a member of the collection. $user = User::load($account_proxy->id()); return AccessResult::allowedIf(Og::isMember($rdf_entity, $user)); }