Skip to content

Commit

Permalink
Merge pull request #2 from Weble/events
Browse files Browse the repository at this point in the history
Notification/Events changed
  • Loading branch information
Skullbock authored Nov 26, 2021
2 parents e8e9190 + 7ab954e commit 8ac6524
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions plugin/src/AlgoliaSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ElementTextareaPro;
use ItemRenderer;
use JFolder;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Menu\MenuItem;
use Joomla\CMS\Router\Route;
Expand All @@ -20,6 +21,7 @@

class AlgoliaSync
{

private App $zoo;
private ItemRenderer $renderer;
private ?SearchClient $client = null;
Expand Down Expand Up @@ -195,20 +197,26 @@ private function algoliaData(\Item $item, $full_related_data = true): ?array
}
}

$this->zoo->event->dispatcher->notify($this->zoo->event->create($item, 'algolia:data', ['data' => &$data]));
$data_override = $this->notify('onYooAlgoliaData', [$item, $data]);

if ($data_override != null) {
return $data_override;
}

return $data;
}

private function elementValueFor(\Element $element, array $params, $full_related_data = true)
{
$value = null;
$this->zoo->event->dispatcher->notify($this->zoo->event->create($element, 'algolia:beforeelementdata', ['value' => &$value]));

$value = $this->notify('onYooAlgoliaBeforeElementData', [$element, $params]);

if ($value !== null) {
return $value;
}


if ($element instanceof \ElementItemName) {
return $element->getItem()->name;
}
Expand Down Expand Up @@ -381,14 +389,26 @@ private function elementValueFor(\Element $element, array $params, $full_related
return $element->getItem()->getTags();
}

if ($value = $element->get('value', null)) {
if ($value = $element->getValue()) {
return $value;
}

$value = $element->getValue();
$this->zoo->event->dispatcher->notify($this->zoo->event->create($element, 'algolia:elementdata', ['value' => &$value]));
return $this->notify('onYooAlgoliaElementData', [$element]);

}

private function notify($event_name, $data)
{
$application = Factory::getApplication();

$value = $application->triggerEvent($event_name, $data);

if (!is_array($value) || count($value) == 0) {
return null;
}

return count($value) > 1 ? array_merge($value) : array_shift($value);

return $value;
}

protected function findMenuItem($type, $id, $lang)
Expand Down

0 comments on commit 8ac6524

Please sign in to comment.