How to Manipulate Attributes

Prerequisites

The Akeneo PIM project introduces services to help you manage your catalog entities.

As stated above, the attribute manager is one of the services that you can get from the Symfony container:

// attribute manager
$this->container->get('pim_catalog.manager.attribute');

In the following examples, we will use $am as the attribute manager service.

Create an Attribute

  • Create a text attribute
// create an attribute
$attribute = $am->createAttribute('pim_catalog_text');
$attribute->setCode('title');
  • Create a simple select attribute

In some cases, you will want to restrain values to a list of possibilities for an attribute. For instance, this example creates a color attribute with a list of predefined options:

$att = $am->createAttribute('pim_catalog_simpleselect');
$att->setCode('color');

$opt1 = $am->createAttributeOption();
$opt1->setCode('purple');
$att->addOption($opt1);

$opt2 = $am->createAttributeOption();
$opt2->setCode('yellow');
$att->addOption($opt2);

$opt3 = $am->createAttributeOption();
$opt3->setCode('blue');
$att->addOption($opt3);
  • Create a simple select attribute with localizable values

Keeping the color example, the value of the option purple is “Purple” in English and “Violet” in French.

$opt1 = $am->createAttributeOption();
$opt1->setCode('purple');
$opt1->setLocalizable(true);

$opt1EN = $am->createAttributeOptionValue();
$opt1EN->setLocale('en_US');
$opt1EN->setValue('Purple');
$opt1->addOptionValue($opt1EN);

$opt1FR = $am->createAttributeOptionValue();
$opt1FR->setLocale('fr_FR');
$opt1FR->setValue('Violet');
$opt1->addOptionValue($opt1FR);

$att->addOption($opt1);
  • Create a localizable attribute
// create a localizable attribute
$attribute = $am->createAttribute('pim_catalog_text');
$attribute->setCode('name');
$attribute->setLocalizable(true);
  • Create a scopable attribute
// create a scopable attribute
$attribute = $am->createAttribute('pim_catalog_text');
$attribute->setCode('image_hd');
$attribute->setScopable(true);
  • Create a localizable AND scopable attribute
// create a localizable and scopable attribute
$attribute = $am->createAttribute('pim_catalog_textarea');
$attribute->setCode('short_description');
$attribute->setScopable(true);
$attribute->setLocalizable(true);