Skip to content

Commit

Permalink
feat: add exportObjectToAlgolia() API for records
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Mar 1, 2022
1 parent c8f47ac commit 5c902bc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
/assets
/resources
composer.lock
.DS_Store
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion src/Service/AlgoliaIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion tests/AlgoliaIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class AlgoliaIndexerTest extends SapphireTest
];

protected static $required_extensions = [
AlgoliaTestObject::class => AlgoliaObjectExtension::class
AlgoliaTestObject::class => [
AlgoliaObjectExtension::class
]
];

public static function setUpBeforeClass()
Expand Down
12 changes: 8 additions & 4 deletions tests/AlgoliaObjectExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class AlgoliaObjectExtensionTest extends SapphireTest
];

protected static $required_extensions = [
AlgoliaTestObject::class => AlgoliaObjectExtension::class
AlgoliaTestObject::class => [
AlgoliaObjectExtension::class
]
];

public static function setUpBeforeClass()
Expand All @@ -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()
Expand All @@ -59,7 +63,7 @@ public function testTouchAlgoliaIndexedDate()
'SELECT AlgoliaIndexed FROM AlgoliaTestObject WHERE ID = %s',
$object->ID
)
)
)->value()
);

$object->touchAlgoliaIndexedDate(true);
Expand All @@ -70,7 +74,7 @@ public function testTouchAlgoliaIndexedDate()
'SELECT AlgoliaIndexed FROM AlgoliaTestObject WHERE ID = %s',
$object->ID
)
)
)->value()
);
}
}
4 changes: 3 additions & 1 deletion tests/AlgoliaQuerierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class AlgoliaQuerierTest extends SapphireTest
];

protected static $required_extensions = [
AlgoliaTestObject::class => AlgoliaObjectExtension::class
AlgoliaTestObject::class => [
AlgoliaObjectExtension::class
]
];

public static function setUpBeforeClass()
Expand Down
4 changes: 0 additions & 4 deletions tests/AlgoliaTestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ public function AbsoluteLink()
return Director::absoluteBaseURL();
}

public function getTitle()
{
return $this->dbObject('Title');
}

public function canIndexInAlgolia(): bool
{
Expand Down

0 comments on commit 5c902bc

Please sign in to comment.