How to Register a New Mass Edit Action on Products

The Akeneo PIM comes with a number of mass edit actions. It also comes with a simple method to define your own mass edit action on selected products.

Work with a custom Acme bundle

Your custom Mass Edit actions have to be in a custom Acme bundle which inherit our EnrichBundle. We advise you to create this custom bundle with the Symfony command:

php app/console generate:bundle

Once your bundle is created, we must inform Symfony it inherits our Bundle:

# src/Acme/Bundle/EnrichBundle/AcmeEnrichBundle.php
<?php

namespace Acme\Bundle\EnrichBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeEnrichBundle extends Bundle
{
    public function getParent()
    {
        return 'PimEnrichBundle';
    }
}

Creating a MassEditAction

The first step is to create a new class that implements MassEditActionInterface or extends ProductMassEditOperation or FamilyMassEditOperation (given on which grid you want to apply the operation):

 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
26
27
# /src/Acme/Bundle/EnrichBundle/MassEditAction/Operation/CapitalizeValues.php
<?php

namespace Acme\Bundle\EnrichBundle\MassEditAction\Operation;

use Pim\Bundle\EnrichBundle\MassEditAction\Operation\ProductMassEditOperation;
use Pim\Bundle\CatalogBundle\Model\ProductInterface;
use Acme\Bundle\EnrichBundle\Form\Type\MassEditAction\CapitalizeValuesType;

class CapitalizeValues extends ProductMassEditOperation
{
    protected $attributeNames = array('sku');

    public function getFormType()
    {
        return new CapitalizeValuesType();
    }

    public function doPerform(ProductInterface $product)
    {
        foreach ($product->getValues() as $value) {
            if (in_array($value->getAttribute()->getCode(), $this->attributeNames)) {
                $value->setData(strtoupper($value->getData()));
            }
        }
    }
}
 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
26
27
28
29
# /src/Acme/Bundle/EnrichBundle/Form/Type/MassEditAction/CapitalizeValuesType.php
<?php

namespace Acme\Bundle\EnrichBundle\Form\Type\MassEditAction;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CapitalizeValuesType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Acme\\Bundle\\EnrichBundle\\MassEditAction\\Operation\\CapitalizeValues'
            )
        );
    }

    public function getName()
    {
        return 'acme_enrich_operation_capitalize_values';
    }
}

This class will contain all the information about the operation to run and the form type which is used to configure it.

Registering the MassEditAction

After the class is created, you must register it as a service in the DIC with the pim_catalog.mass_edit_action tag:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# /src/Acme/Bundle/EnrichBundle/Resources/config/services.yml
services:
    acme_enrich_bundle.mass_edit_action.capitalize_values:
        class: Acme\Bundle\EnrichBundle\MassEditAction\Operation\CapitalizeValues
        arguments:
            - '@pim_catalog.saver.product'
        tags:
            -
              name: pim_enrich.mass_edit_action
              alias: capitalize-values
              operator: pim_enrich.mass_edit_action.operator.product

Note

The alias will be used in the URL (/enrich/mass-edit-action/capitalize-values/configure)

Templating the form of Mass Edit Action

You need to create a template to render your Mass Edit Action form.

1
2
3
4
5
6
7
#  /src/Acme/Bundle/EnrichBundle/Resources/views/MassEditAction/configure/capitalize-values.html.twig
{% extends 'PimEnrichBundle:MassEditAction:configure/layout.html.twig' %}

{% block formContent %}
    My Form is here !
    But we don't have any field for this case.
{% endblock %}

Translating the Mass Edit Action Choice

Once you have realized the previous operations (and eventually cleared your cache), you should see a new option on the /enrich/mass-edit-action/choose page. Akeneo will generate for you a translation key following this pattern: pim_catalog.mass_edit_action.%alias%.label.

You may now define some translation keys (label, description and success_flash) in your translation catalog(s).