Skip to content

Commit

Permalink
Merge pull request #269 from UN-OCHA/berliner/HPC-9904
Browse files Browse the repository at this point in the history
HPC-9904: Fix content search by title
  • Loading branch information
berliner authored Nov 21, 2024
2 parents 5f44b92 + 06b36ca commit cc01341
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\ncms_graphql\Plugin\GraphQL\DataProducer;

use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\ncms_graphql\Wrappers\QueryConnection;
use Drupal\ncms_graphql\Wrappers\ContentExportWrapper;
use Drupal\node\NodeInterface;
use GraphQL\Deferred;

Expand Down Expand Up @@ -62,7 +62,7 @@ public function resolve(array $tags = NULL, FieldContext $context) {
if (!empty($tags)) {
$this->addTagConditionsToQuery($query, $tags);
}
return new QueryConnection($query);
return new ContentExportWrapper($query);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\ncms_graphql\Plugin\GraphQL\DataProducer;

use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\ncms_graphql\Wrappers\QueryConnection;
use Drupal\ncms_graphql\Wrappers\ContentExportWrapper;
use Drupal\node\NodeInterface;
use GraphQL\Deferred;

Expand Down Expand Up @@ -64,7 +64,7 @@ public function resolve(array $tags = NULL, FieldContext $context) {
if (!empty($tags)) {
$this->addTagConditionsToQuery($query, $tags);
}
return new QueryConnection($query);
return new ContentExportWrapper($query);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\ncms_graphql\Plugin\GraphQL\DataProducer;

use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\TranslatableInterface;
Expand All @@ -10,6 +11,7 @@
use Drupal\ncms_graphql\GraphQL\Buffers\EntityMatchingBuffer;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\ncms_graphql\Wrappers\ContentSearchWrapper;
use GraphQL\Deferred;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -174,6 +176,11 @@ public function resolve($type, string $title, ?string $language, ?array $bundles
foreach ($entities as $id => $entity) {
$context->addCacheableDependency($entities[$id]);

if ($entity instanceof EntityPublishedInterface && !$entity->isPublished()) {
unset($entities[$id]);
continue;
}

if (isset($language) && $language !== $entities[$id]->language()->getId() && $entities[$id] instanceof TranslatableInterface) {
$entities[$id] = $entities[$id]->getTranslation($language);
$entities[$id]->addCacheContexts(["static:language:{$language}"]);
Expand All @@ -190,10 +197,7 @@ public function resolve($type, string $title, ?string $language, ?array $bundles
continue;
}
}
return (object) [
'count' => count($entities),
'items' => $entities,
];
return new ContentSearchWrapper($entities);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Drupal\graphql\GraphQL\ResolverRegistry;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
use Drupal\ncms_graphql\Wrappers\QueryConnection;
use Drupal\ncms_graphql\ResultWrapperInterface;
use Drupal\node\NodeInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -683,23 +683,23 @@ private function buildFromComputedTags(ResolverBuilder $builder, $entity_type_id
*/
protected function addListFieldResolvers($type, ResolverRegistry $registry, ResolverBuilder $builder): void {
$registry->addFieldResolver($type, 'count',
$builder->callback(function (QueryConnection $connection) {
return $connection->count();
$builder->callback(function (ResultWrapperInterface $wrapper) {
return $wrapper->count();
})
);
$registry->addFieldResolver($type, 'ids',
$builder->callback(function (QueryConnection $connection) {
return $connection->ids();
$builder->callback(function (ResultWrapperInterface $wrapper) {
return $wrapper->ids();
})
);
$registry->addFieldResolver($type, 'metaData',
$builder->callback(function (QueryConnection $connection) {
return $connection->metaData();
$builder->callback(function (ResultWrapperInterface $wrapper) {
return $wrapper->metaData();
})
);
$registry->addFieldResolver($type, 'items',
$builder->callback(function (QueryConnection $connection) {
return $connection->items();
$builder->callback(function (ResultWrapperInterface $wrapper) {
return $wrapper->items();
})
);
}
Expand Down
42 changes: 42 additions & 0 deletions html/modules/custom/ncms_graphql/src/ResultWrapperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Drupal\ncms_graphql;

/**
* Interface for wrapping results.
*/
interface ResultWrapperInterface {

/**
* Return the number of results.
*
* @return int
* The number of results.
*/
public function count();

/**
* Return the ids.
*
* @return int[]
* An array of ids.
*/
public function ids();

/**
* Return the meta data for all content items.
*
* @return array
* An array of meta data, keyed by content id.
*/
public function metaData();

/**
* Return all items.
*
* @return array|\GraphQL\Deferred
* The promise.
*/
public function items();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Drupal\ncms_graphql\Wrappers;

use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\ncms_graphql\ResultWrapperInterface;
use GraphQL\Deferred;

/**
* Helper class that wraps entity queries.
*/
class QueryConnection {
class ContentExportWrapper implements ResultWrapperInterface {

/**
* The query object.
Expand All @@ -18,7 +19,7 @@ class QueryConnection {
protected $query;

/**
* QueryConnection constructor.
* ContentExportWrapper constructor.
*
* @param \Drupal\Core\Entity\Query\QueryInterface $query
* The query object.
Expand All @@ -28,10 +29,7 @@ public function __construct(QueryInterface $query) {
}

/**
* Return the number of results.
*
* @return int
* The number of results.
* {@inheritdoc}
*/
public function count() {
$query = clone $this->query;
Expand All @@ -41,10 +39,7 @@ public function count() {
}

/**
* Return the ids.
*
* @return int[]
* An array of ids.
* {@inheritdoc}
*/
public function ids() {
$result = $this->query->execute();
Expand All @@ -55,10 +50,7 @@ public function ids() {
}

/**
* Return the article meta data for all article.
*
* @return array
* An array of article meta data, keyed by article id.
* {@inheritdoc}
*/
public function metaData() {
$result = $this->query->execute();
Expand All @@ -79,20 +71,17 @@ public function metaData() {
$field_query->addExpression('FROM_UNIXTIME(n.created)', 'created');
$field_query->addExpression('FROM_UNIXTIME(n.changed)', 'updated');
$field_query->addField('n', 'title');
$field_query->addField('n', 'force_update', 'forceUpdate');
$field_query->addField('summary', 'field_summary_value', 'summary');
$field_query->addField('short_title', 'field_short_title_value', 'title_short');
$field_query->addField('summary', 'field_summary_value', 'summary');
$field_query->addField('n', 'force_update', 'forceUpdate');
$field_query->addField('auto_visible', 'field_automatically_visible_value', 'autoVisible');
$field_query->orderBy('n.changed', 'DESC');
$result = $field_query->execute();
return $result->fetchAllAssoc('id');
}

/**
* Return all items.
*
* @return array|\GraphQL\Deferred
* The promise.
* {@inheritdoc}
*/
public function items() {
$result = $this->query->execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Drupal\ncms_graphql\Wrappers;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\ncms_graphql\ResultWrapperInterface;

/**
* Helper class that wraps entity queries.
*/
class ContentSearchWrapper implements ResultWrapperInterface {

/**
* An array of entitie objects.
*
* @var \Drupal\Core\Entity\ContentEntityInterface[]
*/
protected $entities;

/**
* ContentSearchWrapper constructor.
*
* @param \Drupal\Core\Entity\ContentEntityInterface[] $entities
* The query object.
*/
public function __construct(array $entities) {
$this->entities = $entities;
}

/**
* {@inheritdoc}
*/
public function count() {
return count($this->entities);
}

/**
* {@inheritdoc}
*/
public function ids() {
return array_keys($this->entities);
}

/**
* {@inheritdoc}
*/
public function metaData() {
return array_map(function (ContentEntityInterface $entity) {
return (object) [
'id' => $entity->id(),
'title' => $entity->label(),
'title_short' => $entity->hasField('field_short_title') ? $entity->get('field_short_title')->value : NULL,
'summary' => $entity->hasField('field_summary') ? $entity->get('field_summary')->value : NULL,
'status' => $entity instanceof EntityPublishedInterface ? $entity->isPublished() : 0,
'created' => method_exists($entity, 'getCreatedTime') ? (new \DateTime())->setTimestamp($entity->getCreatedTime())->format(\DateTime::ISO8601) : NULL,
'updated' => $entity instanceof EntityChangedInterface ? (new \DateTime())->setTimestamp($entity->getChangedTime())->format(\DateTime::ISO8601) : NULL,
'autoVisible' => $entity->hasField('field_automatically_visible') ? $entity->get('field_automatically_visible')->value : NULL,
'forceUpdate' => $entity->get('force_update')?->value,
];
}, $this->entities);
}

/**
* {@inheritdoc}
*/
public function items() {
return $this->entities;
}

}

0 comments on commit cc01341

Please sign in to comment.