-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from UN-OCHA/berliner/HPC-9904
HPC-9904: Fix content search by title
- Loading branch information
Showing
7 changed files
with
144 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
html/modules/custom/ncms_graphql/src/ResultWrapperInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
html/modules/custom/ncms_graphql/src/Wrappers/ContentSearchWrapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |