Callbacks
When do I need a callback?
Callbacks get used to converting found values of a CSV file into the required types, which must then get stored in the database.
- The default CSV format of Magento 2 allows, e.g., the following values:
-
-
Catalog
-
Search
-
Catalog, Search
-
Not Visible Individually
-
- Visibility column:
-
The Visibility column expects integer values, those values cannot get stored in the corresponding database column.
-
A callback converts the string to the correct value type; for the Visibility column, the class
TechDivision\\Import\\Product\\Callbacks\\VisibilityCallback
already exists.
-
Default callbacks to convert the standard Magento 2 attributes found in a CSV file get provided by default. |
When a new custom attribute gets added, e.g., a setup script, Pacemaker Import Community tries to find
the best matching callback depends on the attributes frontend_input
value.
- Pacemaker Import Community provides the following
frontend_input
type callbacks by default: -
-
select
-
multiselect
-
boolean
-
Callbacks for additional input types will be part of upcoming releases and can be used or implemented by developers using Pacemaker Import Community in their project at any time.
A callback is necessary to manipulate a VALUE of a specific column per row of a CSV file. Please note that a custom callback replaces the default callback and is not appended. |
How to implement a callback?
To implement a callback, extend the abstract class TechDivision\Import\Callbacks\AbstractCallback.
The callback handle()
method expects the calling observer as a parameter; in return providing the
getAttributeValue()
method allows you to access the value to be processed.
Example
- The callback must return the processed value so that the following callbacks can also process it:
VisibilityCallback.php
<?php
namespace TechDivision\Import\Product\Callbacks;
use TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface;
/**
* A callback implementation that converts the passed visibility.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://github.com/techdivision/import-product
* @link http://www.techdivision.com
*/
class VisibilityCallback extends AbstractCallback
{
/**
* Will be invoked by a observer it has been registered for.
*
* @param \TechDivision\Import\Observers\AttributeCodeAndValueAwareObserverInterface|null $observer The observer
*
* @return mixed The modified value
*/
public function handle(AttributeCodeAndValueAwareObserverInterface $observer = null)
{
// set the observer
$this->setObserver($observer);
// replace the passed attribute value into the visibility ID
return $this->getSubject()->getVisibilityIdByValue($observer->getAttributeValue());
}
}
-
The callback key is the column name; this callback must get called.
-
The callback gets called for each column
visibility
for each row of the processed CSV file.