/**
  * Implements \Drupal\Core\Entity\Query\QueryInterface::notExists().
  */
 public function notExists($property, $langcode = NULL)
 {
     $this->condition->notExists($property, $langcode);
     return $this;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function condition($property, $value = NULL, $operator = '=', $langcode = NULL)
 {
     $key = $property . '-' . $operator;
     $field_storage_definitions = \Drupal::service('entity.manager')->getFieldStorageDefinitions($this->entityTypeId);
     /*
      * Ok, so what is all this:
      * We need to convert our conditions into some sparql compatible conditions.
      */
     $bundle = $this->entityType->getKey('bundle');
     $id = $this->entityType->getKey('id');
     $label = $this->entityType->getKey('label');
     switch ($key) {
         // @todo Limit the graphs here to the set bundles.
         case $bundle . '-IN':
             $rdf_bundles = $this->mappingHandler->getBundleUriList($this->entityType->getBundleEntityType(), $value);
             if ($rdf_bundles) {
                 $this->condition->condition('?entity', '?bundlepredicate', '?type');
                 $this->filterAdded = TRUE;
                 $predicates = "(<" . implode(">, <", $this->entityStorage->bundlePredicate()) . ">)";
                 $this->filter->filter('?bundlepredicate IN ' . $predicates);
                 $this->filter->filter('?type IN ' . $rdf_bundles);
             }
             return $this;
         case $bundle . '-=':
             $mapping = $this->mappingHandler->getRdfBundleMappedUri($this->entityType->getBundleEntityType(), $value);
             $bundle = $mapping[$value];
             if ($bundle) {
                 $this->condition->condition('?entity', '?bundlepredicate', SparqlArg::uri($bundle));
                 $predicates = "(<" . implode(">, <", $this->entityStorage->bundlePredicate()) . ">)";
                 $this->filter->filter('?bundlepredicate IN ' . $predicates);
                 $this->filterAdded = TRUE;
             }
             return $this;
         case $id . '-IN':
             if ($value) {
                 $ids_list = "(<" . implode(">, <", $value) . ">)";
                 if (!$this->filterAdded) {
                     $this->condition->condition('?entity', '?bundlepredicate', '?type');
                     $predicates = "(<" . implode(">, <", $this->entityStorage->bundlePredicate()) . ">)";
                     $this->filter->filter('?bundlepredicate IN ' . $predicates);
                     $this->filterAdded = TRUE;
                 }
                 $this->filter->filter('?entity IN ' . $ids_list);
             }
             return $this;
         case $id . '-NOT IN':
         case $id . '-<>':
             if ($value) {
                 if (is_array($value)) {
                     $ids_list = "(<" . implode(">, <", $value) . ">)";
                 } else {
                     $ids_list = "(<" . $value . ">)";
                 }
                 if (!$this->filterAdded) {
                     $this->condition->condition('?entity', '?bundlepredicate', '?type');
                     $predicates = "(<" . implode(">, <", $this->entityStorage->bundlePredicate()) . ">)";
                     $this->filter->filter('?bundlepredicate IN ' . $predicates);
                     $this->filterAdded = TRUE;
                 }
                 $this->filter->filter('!(?entity IN ' . $ids_list . ')');
             }
             return $this;
         case $id . '-=':
             if (!$value) {
                 return $this;
             }
             $id = '<' . $value . '>';
             if (!$this->filterAdded) {
                 $this->condition->condition('?entity', '?bundlepredicate', '?type');
                 $predicates = "(<" . implode(">, <", $this->entityStorage->bundlePredicate()) . ">)";
                 $this->filter->filter('?bundlepredicate IN ' . $predicates);
                 $this->filterAdded = TRUE;
             }
             $this->filter->filter('?entity IN ' . SparqlArg::literal($id));
             break;
         case $label . '-=':
             preg_match('/\\((.*?)\\)/', $value, $matches);
             $matching = array_pop($matches);
             if ($matching) {
                 $ids = "(<{$matching}>)";
                 $this->filter->filter('?entity IN ' . $ids);
             } else {
                 if (file_valid_uri($value)) {
                     $ids = "(<{$value}>)";
                     $this->filter->filter('?entity IN ' . $ids);
                 } else {
                     $mapping = $this->mappingHandler->getEntityTypeLabelPredicates($this->entityTypeId);
                     $label_list = "(<" . implode(">, <", array_unique(array_keys($mapping))) . ">)";
                     $this->condition->condition('?entity', '?label_type', '?label');
                     $this->filter->filter('?label_type IN ' . $label_list);
                     $this->filter->filter('str(?label) = "' . $value . '"');
                 }
             }
             return $this;
         case $label . '-CONTAINS':
             $mapping = $this->mappingHandler->getEntityTypeLabelPredicates($this->entityTypeId);
             $label_list = "(<" . implode(">, <", array_unique(array_keys($mapping))) . ">)";
             $this->condition->condition('?entity', '?label_type', '?label');
             $this->filter->filter('?label_type IN ' . $label_list);
             if ($value) {
                 $this->filter->filter('regex(?label, "' . $value . '", "i")');
                 $this->filter->filter('(lang(?label) = "" || langMatches(lang(?label), "EN"))');
             }
             return $this;
         case '_field_exists-EXISTS':
         case '_field_exists-NOT EXISTS':
             $field_rdf_name = $this->getFieldRdfPropertyName($value, $field_storage_definitions);
             if (!UrlHelper::isValid($field_rdf_name, TRUE) === FALSE) {
                 $field_rdf_name = SparqlArg::uri($field_rdf_name);
             }
             if ($field_rdf_name) {
                 $this->filter('?entity ' . $field_rdf_name . ' ?c', 'FILTER ' . $operator);
             }
             return $this;
     }
     if ($operator == '=') {
         if (!$value) {
             return $this;
         }
         // @todo this code will be handled in ISAICP-2631
         if (strpos($property, '.') !== FALSE) {
             list($field_name, $column) = explode('.', $property);
         } else {
             $field_name = $property;
         }
         $field_rdf_name = $this->getFieldRdfPropertyName($field_name, $field_storage_definitions);
         if (!UrlHelper::isValid($value, TRUE) === FALSE) {
             $value = SparqlArg::uri($value);
         } else {
             $value = SparqlArg::literal($value);
         }
         $this->condition->condition('?entity', SparqlArg::uri($field_rdf_name), $value);
     }
     return $this;
 }