From 45725141af2116cf6c1363cc0c97489b950eef55 Mon Sep 17 00:00:00 2001 From: jsblair9 Date: Wed, 22 May 2019 09:30:29 -0500 Subject: [PATCH] :sparkles: allow multiple collections | close #1 --- README.md | 11 ++++++-- .../UpdateCollectionSearchListener.php | 25 ++++++++++++++----- UpdateCollectionSearch/meta.yaml | 2 +- UpdateCollectionSearch/settings.yaml | 3 +-- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 89e5acd..9aa71f4 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,13 @@ Automatically update a collection's search index when an entry is saved NOTE: -- Must select the collection in the addon settings -- Only accepts one collection right now (open an FR issue if you want more than one) +- Must select the collection(s) in addon settings +- `v1.1` now accepts more than one collection! - and you must make sure the collection is set up for indexing per the [Statamic Docs](https://docs.statamic.com/search#collection-indexes) +- This only matters if you're using the `collection` parameter on the [search results tag](https://docs.statamic.com/tags/search#parameters) +``` + {{ search:results collection="foo" }} + ... + {{ /search:results }} +``` +Otherwise, collections are part of the default index and is updated automatically if Search auto-index is enabled in your settings. diff --git a/UpdateCollectionSearch/UpdateCollectionSearchListener.php b/UpdateCollectionSearch/UpdateCollectionSearchListener.php index 1c168d6..6ac0fd1 100755 --- a/UpdateCollectionSearch/UpdateCollectionSearchListener.php +++ b/UpdateCollectionSearch/UpdateCollectionSearchListener.php @@ -9,18 +9,31 @@ class UpdateCollectionSearchListener extends Listener { /** - * The events to be listened for, and the methods to call. + * Listen for entries being published and then update the search index * * @var array */ - public $events = ['cp.entry.published' => 'handleAgentAdded']; + public $events = ['cp.entry.published' => 'handleEntrySaved']; - public function handleAgentAdded(Entry $entry) + public function handleEntrySaved(Entry $entry) { - if ($entry->collectionName() == $this->getConfig('collection_to_index', null)) { - $index = 'collections/' . $this->getConfig('collection_to_index', null); - + $collection_name = $entry->collectionName(); + + if ($this->shouldUpdateCollection($collection_name)) { + $index = 'collections/' . $collection_name; Search::update($index); } } + + /** + * Only update the collections if in the config + * + * @param $formset_name string + * + * @return bool + */ + private function shouldUpdateCollection($collection_name) + { + return collect($this->getConfig('collection_to_index'))->contains($collection_name); + } } \ No newline at end of file diff --git a/UpdateCollectionSearch/meta.yaml b/UpdateCollectionSearch/meta.yaml index 50a4a02..19130c5 100644 --- a/UpdateCollectionSearch/meta.yaml +++ b/UpdateCollectionSearch/meta.yaml @@ -1,5 +1,5 @@ name: Update Collection Search -version: 1.0 +version: 1.1 description: Update a collection's search index when addding a new entry to it addon_url: null developer: LionsMouth Digital diff --git a/UpdateCollectionSearch/settings.yaml b/UpdateCollectionSearch/settings.yaml index 6d5e20e..d883a0a 100644 --- a/UpdateCollectionSearch/settings.yaml +++ b/UpdateCollectionSearch/settings.yaml @@ -1,4 +1,3 @@ fields: collection_to_index: - type: collections - max_items: 1 \ No newline at end of file + type: collections \ No newline at end of file