Integrations / Platforms / Magento 2 / Dispatched Back-End Events

Dispatched Back-End Events

We provide events so developers can customize the behavior of their extension. We utilize Magento’s dispatched events convention for our hooks. You can find a full list of our dispatched events at the end of the page.

You can learn how to create listeners on custom events in a custom extension in Create a custom extension tutorial.

Modifying index settings

All indices managed by the extension have a *_index_before_set_settings event that lets you modify index settings before sending them to Algolia.

Entity Related Event
products algolia_products_index_before_set_settings
categories algolia_categories_index_before_set_settings
pages algolia_pages_index_before_set_settings
suggestions algolia_suggestions_index_before_set_settings
additional_sections algolia_additional_sections_index_before_set_settings

Here’s an example of how this kind of event is dispatched in the extension:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$indexSettings = [...];

// Additional index settings from event observer
$transport = new DataObject($indexSettings);

$this->eventManager->dispatch(
    'algolia_products_index_before_set_settings',
    [
        'store_id'       => $storeId,
        'index_settings' => $transport,
    ]
);

$indexSettings = $transport->getData();
$this->algoliaHelper->setSettings($indexName, $indexSettings, false, true);

Whenever using backend events, make sure to refer to where it is dispatched in the code. This lets you see what variable are available for your customization.

You can add or modify the index settings parameters object before sending it to Algolia. Keep in mind that at this point of the event, that the extension has already added and formatted parameters based on your settings.

Here’s an example of how an observer could modify index settings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php 

namespace Your\CustomModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AlgoliaUpdateProductsSettings implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $productsSettings = $observer->getData('index_settings');
        $searchableAttributes = $productSettings->getData('searchableAttributes');
        
        // Add code here to modify searchableAttributes
    
        // reset the searchableAttributes value with the updated array
        $productsSettings->setData('searchableAttributes', $searchableAttributes);
    
        // add unhandled setting
        $productsSettings->setData('snippetEllipsisText', '...')  
    }
}

Updating attribute data

If you need to modify entity data just before indexing, the extension offers algolia_after_create_*_object events to let you add your changes to almost all the maintained entities.

Entity Related Event
products algolia_after_create_product_object
categories algolia_after_create_category_object
pages algolia_after_create_page_object
suggestions algolia_after_create_suggestion_object

These events fire in the getObject() method of the entity Helper class. For example, in \Algolia\AlgoliaSearch\Helper\Entity\ProductHelper::getObject() you can see this event is dispatched just before the object is returned for indexing:

1
2
3
4
5
6
$transport = new DataObject($customData);
$this->eventManager->dispatch(
    'algolia_after_create_product_object',
    ['custom_data' => $transport, 'sub_products' => $subProducts, 'productObject' => $product]
);
$customData = $transport->getData();

Here’s an example of how an observer could transform the created_at attribute from the default MySQL date format to a timestamp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php 

namespace Your\CustomModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AlgoliaAfterCreateProductObject implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $data = $observer->getData('custom_data');
    
        // update created_at MySQL date format to timestamp 
        $createdAt = $data->getData('created_at');
        $data->setData('created_at', strtotime($createdAt));
    }
}

Modifying Product Data

As previously seen, algolia_after_create_product_object is the event to use to modify the product data sent to Algolia. However, depending on what data you need for your customization, a particular attribute may not be available in the collection data.

In that case, you need go farther back in the stack trace to modify the product collection data to request the attribute you need. The reason is that the product data we use for indexing is from a product collection request. Since we specify the attributes for this collection, the full product model is not available during the object creation method.

This applies for categories and suggestions indexing as well. They have their own algolia_after_*_collection_build events where you can access the collection.

The\Algolia\AlgoliaSearch\Helper\Entity\ProductHelper::getProductCollectionQuery() method is where we create the product collection and set its attributes. As you can see, we dispatch the algolia_after_products_collection_build event where you have access to the $collection object, at the end of the method:

1
2
3
4
5
6
7
8
9
$this->eventManager->dispatch(
    'algolia_after_products_collection_build',
    [
        'store' => $storeId,
        'collection' => $products,
        'only_visible' => $onlyVisible,
        'include_not_visible_individually' => $includeNotVisibleIndividually,
    ]
);

In your custom observer, you can access the product collection and add your attribute before the collection loads:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php 

namespace Your\CustomModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AlgoliaAddAttributesToProductCollection implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $collection = $observer->getData('collection');
        $collection->addAttributeToSelect('custom_attribute_code');
    }
}

Once you’ve added the attribute, your product can now retrieve it in your observer for the algolia_after_create_product_object event:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php 

namespace Your\CustomModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AlgoliaAfterCreateProductObject implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $data = $observer->getData('custom_data');
        $product = $observer->getData('productObject');

        // add new custom attribute
        $data->setData('newCustomAttribute', $product->getData('custom_attribute_code'));

        // update created_at MySQL date format to timestamp
        $createdAt = $data->getData('created_at');
        $data->setData('created_at', strtotime($createdAt));
    }
}

In the example above, we added our new customization along with the previous one for transforming data. It’s up to you if you’d like to use one observer for one event for multiple customizations, or if you’d like to create a separate observer for each customization.

Changing front-end configuration

In order for the front end to display the configured UI, the extension creates the algoliaConfig variable in JavaScript by retrieving the configuration from a block class. You can modify the algoliaConfig object before page load by using the algolia_after_create_configuration event.

This event is dispatched at the end of \Algolia\AlgoliaSearch\Block\Configuration::getConfiguration():

1
2
3
4
5
6
$transport = new DataObject($algoliaJsConfig);
$this->_eventManager->dispatch(
    'algolia_after_create_configuration', 
    ['configuration' => $transport]
);
$algoliaJsConfig = $transport->getData();

As you can see, we turn the configuration array into a DataObject so that you can easily make modifications in your observer using getters and setters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php 

namespace Your\CustomModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AlgoliaAfterCreateConfiguration implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $config = $observer->getData('configuration');
        // add new config value
        $config->setData('newData', 'newValue');

        // modify set value 9 -> 10
        $config->setData('hitsPerPage', 10);

        // modify/add new translations
        $trans = $config->getData('translations');
        $trans['newTranslation'] = __('New Translation');
        $trans['showMore'] = __('Show More');
        $config->setData('translations', $trans);
    }
}

List of events

Product events

Dispatching before pushing products’ index settings to Algolia

algolia_products_index_before_set_settings

Passed parameters:

  • $storeId
  • $index_settings as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $index_settings as data in Magento\Framework\DataObject object

Dispatching after products collection creation

algolia_after_products_collection_build

Passed parameters:

  • $storeId
  • $collection as Magento\Catalog\Model\ResourceModel\Product\Collection
  • $only_visible
  • $include_not_visible_individually

Modifiable parameters:

  • $collection as Magento\Catalog\Model\ResourceModel\Product\Collection

Dispatching before final products collection load

algolia_before_products_collection_load

Passed parameters:

  • $collection as Magento\Catalog\Model\ResourceModel\Product\Collection
  • $store

Modifiable parameters:

  • $collection

Dispatching before fetching product’s attributes for indexing

algolia_product_index_before

Passed parameters:

  • $product as Magento\Catalog\Model\Product
  • $custom_data as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $custom_data as data in Magento\Framework\DataObject object

Dispatching after fetching product’s attributes for indexing

algolia_after_create_product_object

Passed parameters:

  • $custom_data as data in Magento\Framework\DataObject object
  • $subProducts as array of sub products
  • $productObject as Magento\Catalog\Model\Product

Modifiable parameters:

  • $custom_data as data in Magento\Framework\DataObject object

Category events

Dispatching before pushing categories’ index settings to Algolia

algolia_categories_index_before_set_settings

Passed parameters:

  • $storeId
  • $index_settings as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $index_settings as data in Magento\Framework\DataObject object

Dispatching after categories collection creation

algolia_after_categories_collection_build

Passed parameters:

  • $store
  • $collection as Magento\Catalog\Model\ResourceModel\Category\Collection

Modifiable parameters:

  • $collection

Dispatching before fetching category’s attributes for indexing

algolia_category_index_before

Passed parameters:

  • $category as Magento\Catalog\Model\Category
  • $custom_data as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $custom_data as data in Magento\Framework\DataObject object

Dispatching after fetching category’s attributes for indexing

algolia_after_create_category_object

Passed parameters:

  • $category as Magento\Catalog\Model\Category
  • $categoryObject as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $categoryObject as data in Magento\Framework\DataObject object

Page events

Dispatching before pushing pages’ index settings to Algolia

algolia_pages_index_before_set_settings

Passed parameters:

  • $store_id
  • $index_settings as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $index_settings as data in Magento\Framework\DataObject object

Dispatching after fetching page’s attributes for indexing

algolia_after_create_page_object

Passed parameters:

  • $page as data in Magento\Framework\DataObject object
  • $pageObject as Magento\Cms\Model\Page

Modifiable parameters:

  • $page as data in Magento\Framework\DataObject object

Suggestion events

Dispatching before pushing suggestions’ index settings to Algolia

algolia_suggestions_index_before_set_settings

Passed parameters:

  • $store_id
  • $index_settings as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $index_settings as data in Magento\Framework\DataObject object

Dispatching after suggestions collection creation

algolia_after_suggestions_collection_build

Passed parameters:

  • $store
  • $collection as Magento\Search\Model\ResourceModel\Query\Collection

Modifiable parameters:

  • $collection

Dispatching after fetching suggestion’s attributes for indexing

algolia_after_create_suggestion_object

Passed parameters:

  • $suggestion as data in Magento\Framework\DataObject object
  • $suggestionObject as Magento\Search\Model\Query

Modifiable parameters:

  • $suggestion as data in Magento\Framework\DataObject object

Additional sections events

Dispatching before pushing additional sections’ index settings to Algolia

algolia_additional_sections_index_before_set_settings

Passed parameters:

  • $store_id
  • $index_settings as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $index_settings as data in Magento\Framework\DataObject object

Dispatching after fetching additional_section’s attributes for indexing

algolia_additional_section_items_before_index

Passed parameters:

  • $section
  • $record as data in Magento\Framework\DataObject object
  • $store_id

Modifiable parameters:

  • $record as data in Magento\Framework\DataObject object

Front-end configuration events

Dispatching after front-end configuration creation

algolia_after_create_configuration

Passed parameters:

  • $configuration as data in Magento\Framework\DataObject object

Modifiable parameters:

  • $configuration as data in Magento\Framework\DataObject object
Did you find this page helpful?