Callbacks

When do I need a callback?

Callbacks can be used to transform values found in the CSV file into the necessary types that need to be stored in the database.

For example, the default Magento 2 CSV format allows the values

  • Catalog

  • Search

  • Catalog, Search

  • Not Visible Individually

For the column visibility. These values can not be stored in the appropriate database column, as this expects integer values.

Therefore, a callback can transform the string into the correct integer value; in this case, the class TechDivision\\Import\\Product\\Callbacks\\VisibilityCallback.

The necessary callbacks to transform the Magento 2 standard attributes found in the CSV file are already defined by default.

When a new, user-defined attribute will be added, e.g. with a setup script, the Pacemaker Community Edition (CE) tries to find the best matching callback, depending on the attribute’s frontend_input value. Pacemaker Community Edition (CE) comes with callbacks for

  • select

  • multiselect

  • boolean

frontend_input types. Callbacks for other input types will be part of upcoming versions and can always be implemented by the developers using Pacemaker Community Edition (CE) in their project.

In general, you need a callback if you want to do something with the VALUE on a specific column in each row of a CSV file.

Please be aware that a custom callback will replace the default callback and not be appended!

How to implement a callback?

To implement a callback, you can extend the abstract class TechDivision\Import\Callbacks\AbstractCallback.

The callback’s handle() method expects the invoking observer as a parameter that provides the method getAttributeValue that you access to the value you want to process.

The callback must returns the processed value to allow the following callbacks also to process it.

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());
    }
}

To register a callback, it has to be added to the array with the callbacks of a subject, like

"callbacks": [
  {
    "visibility": [
      "import_product.callback.visibility"
    ]
  }
]

In our casevisibility, the callback key is the column name; the callback has to be invoked.

Therefore, the callback will be invoked on every column visibility for each row of the processed CSV file.