/** * Returns the RDF entity with the given name and type. * * If multiple RDF entities have the same name the first one will be returned. * * @param string $label * The RDF entity label. * @param string $type * Optional RDF entity type. * * @return \Drupal\rdf_entity\Entity\Rdf * The RDF entity. * * @throws \InvalidArgumentException * Thrown when an RDF entity with the given name and type does not exist. */ protected function getRdfEntityByLabel($label, $type = NULL) { $query = \Drupal::entityQuery('rdf_entity')->condition('label', $label)->range(0, 1); if (!empty($type)) { $query->condition('rid', $type); } $result = $query->execute(); if (empty($result)) { $message = $type ? "The {$type} entity with the label '{$label}' was not found." : "The RDF entity with the label '{$label}' was not found."; throw new \InvalidArgumentException($message); } return Rdf::load(reset($result)); }
/** * {@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']); }
/** * {@inheritdoc} */ public function validate($items, Constraint $constraint) { if (!($item = $items->first())) { return; } $field_name = $items->getFieldDefinition()->getName(); /** @var \Drupal\rdf_entity\RdfInterface $entity */ $entity = $items->getEntity(); $entity_type_id = $entity->getEntityTypeId(); $id_key = $entity->getEntityType()->getKey('id'); // Check first for the release. if ($entity->bundle() == 'asset_release') { // Get the solution this entity belongs to. $parent = Rdf::load($entity->get('field_isr_is_version_of')->getValue()[0]['target_id']); // The release can have the same name as the solution it belongs to. if ($parent->label() == $entity->label()) { return; } // The release can have the same name as the sibling releases. foreach ($parent->get('field_is_has_version')->getValue() as $release) { $sibling = Rdf::load($release['target_id']); if ($entity->label() == $sibling->label()) { return; } } } $query = \Drupal::entityQuery($entity_type_id)->condition($field_name, $item->value)->condition('rid', $entity->bundle()); if (!empty($entity->id())) { $query->condition($id_key, $items->getEntity()->id(), '<>'); } // If this is a solution, ignore releases. if ($entity->bundle() == 'solution') { $query->notExists('field_isr_is_version_of'); } $value_taken = (bool) $query->range(0, 1)->count()->execute(); if ($value_taken) { $this->context->addViolation($constraint->message, ['%value' => $item->value, '@entity_type' => $entity->getEntityType()->getLowercaseLabel(), '@field_name' => Unicode::strtolower($items->getFieldDefinition()->getLabel())]); } }
/** * {@inheritdoc} */ public function getRuntimeContexts(array $unqualified_context_ids) { $result = []; $value = NULL; if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['rdf_entity'])) { /** @var \Drupal\rdf_entity\RdfInterface $collection */ if ($collection = $this->routeMatch->getParameter('rdf_entity')) { if ($collection->bundle() == 'collection') { $value = $collection; } } } elseif (($route_parameters = $this->routeMatch->getParameters()) && in_array($this->routeMatch->getRouteName(), $this->getSupportedRoutes())) { foreach ($route_parameters as $route_parameter) { if ($route_parameter instanceof ContentEntityInterface) { $bundle = $route_parameter->bundle(); $entity_type = $route_parameter->getEntityTypeId(); // Check if the object is a og content entity. if (Og::isGroupContent($entity_type, $bundle) && ($groups = $this->membershipManager->getGroupIds($route_parameter, 'rdf_entity', 'collection'))) { // A content can belong to only one rdf_entity. // Check that the content is not an orphaned one. if ($collection_id = reset($groups['rdf_entity'])) { $collection = Rdf::load($collection_id); $value = $collection; } } } } } $cacheability = new CacheableMetadata(); $cacheability->setCacheContexts(['route']); $collection_context_definition = new ContextDefinition('entity', $this->t('Organic group provided by collection'), FALSE); $context = new Context($collection_context_definition, $value); $context->addCacheableDependency($cacheability); $result['og'] = $context; return $result; }
/** * Returns the owner entity of this node if it exists. * * The news entity can belong to a collection or a solution, depending on * how it was created. This function will return the parent of the entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The news content entity. * * @return \Drupal\rdf_entity\RdfInterface|null * The parent of the entity. This can be a collection or a solution. * If there is no parent found, return NULL. * * @todo This is currently called in joinup_news_node_access() as a workaround * for the lack of access checking in OG. Turn this in a protected method as * soon as this is fixed in OG. * * @see joinup_news_node_access() * @see https://github.com/amitaibu/og/pull/217 * @see https://webgate.ec.europa.eu/CITnet/jira/browse/ISAICP-2622 */ public static function getParent(EntityInterface $entity) { $parent = NULL; if (!empty($entity->{OgGroupAudienceHelperInterface::DEFAULT_FIELD}->first()->target_id)) { /** @var \Drupal\rdf_entity\RdfInterface $parent */ $parent = Rdf::load($entity->{OgGroupAudienceHelperInterface::DEFAULT_FIELD}->first()->target_id); } return $parent; }