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.

Creating a MassEditAction

The first step is to create a new class that implements MassEditActionInterface:

 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
30
31
32
33
34
35
36
37
38
39
40
# /src/Acme/Bundle/EnrichBundle/MassEditAction/CapitalizeValues.php
<?php

namespace Acme\Bundle\EnrichBundle\MassEditAction;

use Doctrine\ORM\QueryBuilder;
use Pim\Bundle\EnrichBundle\MassEditAction\MassEditActionInterface;
use Acme\Bundle\EnrichBundle\Form\Type\MassEditAction\CapitalizeValuesType;

class CapitalizeValues implements MassEditActionInterface
{
    protected $attributeNames = array('sku');

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

    public function getFormOptions()
    {
        return array();
    }

    public function initialize(QueryBuilder $qb)
    {
    }

    public function perform(QueryBuilder $qb)
    {
        $products = $qb->getQuery()->getResult();

        foreach ($products as $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\\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
# /src/Acme/Bundle/EnrichBundle/Resources/config/services.yml
services:
    acme_enrich_bundle.mass_edit_action.capitalize_values:
        class: Acme\Bundle\EnrichBundle\MassEditAction\CapitalizeValues
        tags:
            - { name: pim_enrich.mass_edit_action, alias: capitalize-values }

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.

Note

The template must be in /src/Acme/Bundle/EnrichBundle/Resources/views/MassEditAction/configure/

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).