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