diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..588d9a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/vendor +/assets +/resources +composer.lock +.DS_Store diff --git a/README.md b/README.md index 3472b62..a9fb5b5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # :mag: Silverstripe Algolia Module -[![Build Status](http://img.shields.io/travis/wilr/silverstripe-algolia.svg?style=flat-square)](http://travis-ci.org/wilr/silverstripe-algolia) +[![Build Status](http://img.shields.io/travis/wilr/silverstripe-algolia.svg?style=flat-square)](http://travis-ci.com/wilr/silverstripe-algolia) [![codecov](https://codecov.io/gh/wilr/silverstripe-algolia/branch/master/graph/badge.svg)](https://codecov.io/gh/wilr/silverstripe-algolia) [![Version](http://img.shields.io/packagist/v/wilr/silverstripe-algolia.svg?style=flat-square)](https://packagist.org/packages/wilr/silverstripe-algolia) [![License](http://img.shields.io/packagist/l/wilr/silverstripe-algolia.svg?style=flat-square)](LICENSE) @@ -173,6 +173,23 @@ class MyPage extends Page { } ``` +Or, you can define a `exportObjectToAlgolia` method on your object. This +receives the default index fields and then allows you to add or remove fields +as required + +```php +class MyPage extends Page { + + public function exportObjectToAlgolia($data) + { + return array_merge($data, [ + 'MyCustomField' => $this->MyCustomField(), + 'Something' => $this->Object()->Name, + ]); + } +} +``` + ### Customising the indexed relationships Out of the box, the default is to push the ID and Title fields of any diff --git a/src/Service/AlgoliaIndexer.php b/src/Service/AlgoliaIndexer.php index 0cecfd6..1506942 100644 --- a/src/Service/AlgoliaIndexer.php +++ b/src/Service/AlgoliaIndexer.php @@ -110,7 +110,29 @@ public function indexItems($items) } /** - * Generates a map of all the fields and values which will be sent. + * Generates a map of all the fields and values which will be sent. Two ways + * to modifty the attributes sent to algolia. Either define the properties + * via the config API + * + * ``` + * private static $algolia_index_fields = [ + * 'MyCustomField' + * ]; + * ``` + * + * Or, use exportObjectToAlgolia to return an array + * + * ``` + * class MyObject extends DataObject + * { + * public function exportObjectToAlgolia($data) + * { + * return array_merge($data, [ + * 'MyCustomField' => $this->MyCustomField() + * ]); + * } + * } + * ``` * * @param DataObject * @@ -130,6 +152,10 @@ public function exportAttributesFromObject($item) 'objectLink' => str_replace(['?stage=Stage', '?stage=Live'], '', $item->AbsoluteLink()) ]; + if ($item && $item->hasMethod('exportObjectToAlgolia')) { + return $item->exportObjectToAlgolia($toIndex); + } + if ($this->config()->get('include_page_content')) { $toIndex['objectForTemplate'] = Injector::inst()->create(AlgoliaPageCrawler::class, $item)->getMainContent(); diff --git a/tests/AlgoliaIndexerTest.php b/tests/AlgoliaIndexerTest.php index 3efd555..5298c32 100644 --- a/tests/AlgoliaIndexerTest.php +++ b/tests/AlgoliaIndexerTest.php @@ -18,7 +18,9 @@ class AlgoliaIndexerTest extends SapphireTest ]; protected static $required_extensions = [ - AlgoliaTestObject::class => AlgoliaObjectExtension::class + AlgoliaTestObject::class => [ + AlgoliaObjectExtension::class + ] ]; public static function setUpBeforeClass() diff --git a/tests/AlgoliaObjectExtensionTest.php b/tests/AlgoliaObjectExtensionTest.php index c80b174..3806d13 100644 --- a/tests/AlgoliaObjectExtensionTest.php +++ b/tests/AlgoliaObjectExtensionTest.php @@ -18,7 +18,9 @@ class AlgoliaObjectExtensionTest extends SapphireTest ]; protected static $required_extensions = [ - AlgoliaTestObject::class => AlgoliaObjectExtension::class + AlgoliaTestObject::class => [ + AlgoliaObjectExtension::class + ] ]; public static function setUpBeforeClass() @@ -43,7 +45,9 @@ public function testIndexInAlgolia() $this->assertTrue($object->canIndexInAlgolia(), 'Objects with canIndexInAlgolia() set to true should index'); - $this->assertTrue($object->indexInAlgolia(), 'Indexed in Algolia'); + $index = $object->indexInAlgolia(); + + $this->assertTrue($index, 'Indexed in Algolia'); } public function testTouchAlgoliaIndexedDate() @@ -59,7 +63,7 @@ public function testTouchAlgoliaIndexedDate() 'SELECT AlgoliaIndexed FROM AlgoliaTestObject WHERE ID = %s', $object->ID ) - ) + )->value() ); $object->touchAlgoliaIndexedDate(true); @@ -70,7 +74,7 @@ public function testTouchAlgoliaIndexedDate() 'SELECT AlgoliaIndexed FROM AlgoliaTestObject WHERE ID = %s', $object->ID ) - ) + )->value() ); } } diff --git a/tests/AlgoliaQuerierTest.php b/tests/AlgoliaQuerierTest.php index 1255aa1..231b585 100644 --- a/tests/AlgoliaQuerierTest.php +++ b/tests/AlgoliaQuerierTest.php @@ -20,7 +20,9 @@ class AlgoliaQuerierTest extends SapphireTest ]; protected static $required_extensions = [ - AlgoliaTestObject::class => AlgoliaObjectExtension::class + AlgoliaTestObject::class => [ + AlgoliaObjectExtension::class + ] ]; public static function setUpBeforeClass() diff --git a/tests/AlgoliaTestObject.php b/tests/AlgoliaTestObject.php index fb0b5d3..967fc77 100644 --- a/tests/AlgoliaTestObject.php +++ b/tests/AlgoliaTestObject.php @@ -41,10 +41,6 @@ public function AbsoluteLink() return Director::absoluteBaseURL(); } - public function getTitle() - { - return $this->dbObject('Title'); - } public function canIndexInAlgolia(): bool {