Skip to content

Commit

Permalink
Add support for non-db object attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed May 19, 2021
1 parent 08ea9db commit dee6673
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Service/AlgoliaIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
use Exception;
use LogicException;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\ClassInfo;
Expand Down Expand Up @@ -136,15 +137,28 @@ public function exportAttributesFromObject($item)
$specs = $item->config()->get('algolia_index_fields');

if ($specs) {
$maxFieldSize = $this->config()->get('max_field_size_bytes');

foreach ($specs as $attributeName) {
if (in_array($attributeName, $this->config()->get('attributes_blacklisted'))) {
continue;
}

$dbField = $item->relObject($attributeName);
$maxFieldSize = $this->config()->get('max_field_size_bytes');
// fetch the db object, or fallback to the getters but prefer
// the db object
try {
$dbField = $item->relObject($attributeName);
} catch (LogicException $e) {
$dbField = $item->{$attributeName};
}

if (!$dbField) {
continue;
}

if ($dbField && ($dbField->exists() || $dbField instanceof DBBoolean)) {
if (is_string($dbField) || is_array($dbField)) {
$attributes->push($attributeName, $dbField);
} elseif ($dbField->exists() || $dbField instanceof DBBoolean) {
if ($dbField instanceof RelationList || $dbField instanceof DataObject) {
// has-many, many-many, has-one
$this->exportAttributesFromRelationship($item, $attributeName, $attributes);
Expand Down

0 comments on commit dee6673

Please sign in to comment.