Ejemplo n.º 1
0
 /**
  * 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));
 }
Ejemplo n.º 2
0
 /**
  * {@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']);
 }
 /**
  * Builds the overview of the licence entities.
  *
  * This is mimicking the rdf_entity's overview builder class.
  *
  * @see Drupal\rdf_entity\Entity\Controller\RdfListBuilder
  *
  * @return string
  *   Return Hello string.
  */
 public function overview()
 {
     $query = $this->entityStorage->getQuery()->condition('rid', 'licence');
     $header = $this->buildHeader();
     $query->tableSort($header);
     $rids = $query->execute();
     $licences = Rdf::loadMultiple($rids);
     $rows = [];
     foreach ($licences as $licence) {
         $rows[] = $this->buildRow($licence);
     }
     $build['table'] = array('#type' => 'table', '#header' => $this->buildHeader(), '#rows' => array(), '#empty' => $this->t('There are no licences yet.'));
     foreach ($licences as $licence) {
         if ($row = $this->buildRow($licence)) {
             $build['table']['#rows'][$licence->id()] = $row;
         }
     }
     return $build;
 }
 /**
  * {@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;
 }
Ejemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function build()
 {
     // If the page is not a solution page, return an empty form so that the
     // rendering of this block can be omitted.
     if (empty($this->solution) || $this->solution->bundle() != 'solution') {
         return [];
     }
     // Get news referencing to this solution.
     // @todo EntityManager is deprecated. Use EntityTypeManager instead.
     // @see https://webgate.ec.europa.eu/CITnet/jira/browse/ISAICP-2669
     $entities = $this->entityManager->getStorage('node')->loadByProperties([OgGroupAudienceHelperInterface::DEFAULT_FIELD => $this->solution->id()]);
     $items = [];
     foreach ($entities as $entity) {
         $items[] = ['#markup' => $entity->link()];
     }
     // Also retrieve related collections.
     $ids = $this->entityManager->getStorage('rdf_entity')->getQuery()->condition('field_ar_affiliates', $this->solution->id())->execute();
     $entities = Rdf::loadMultiple($ids);
     foreach ($entities as $entity) {
         $items[] = ['#markup' => $entity->link()];
     }
     // Build the array output.
     if ($items) {
         return ['list' => ['#theme' => 'item_list', '#items' => $items, '#cache' => ['tags' => ['entity:node:news', 'entity:rdf_entity:collection']]]];
     }
     return [];
 }
 /**
  * Returns a build array for the solution releases overview page.
  *
  * @param \Drupal\rdf_entity\RdfInterface $rdf_entity
  *   The solution rdf entity.
  *
  * @return array
  *   The build array for the page.
  */
 public function overview(RdfInterface $rdf_entity)
 {
     $view_builder = $this->entityTypeManager()->getViewBuilder('rdf_entity');
     $ids = $this->queryFactory->get('rdf_entity', 'AND')->condition('rid', 'asset_release')->condition('field_isr_is_version_of', $rdf_entity->id())->sort('field_isr_creation_date', 'DESC')->execute();
     $releases = [];
     /** @var \Drupal\rdf_entity\RdfInterface $release */
     foreach (Rdf::loadMultiple($ids) as $release) {
         $releases[] = $view_builder->view($release, 'compact');
     }
     return ['#theme' => 'asset_release_releases_download', '#releases' => $releases];
 }