Find and reference standard attribute types in Akeneo code

All standard attribute types available in Akeneo are listed in “\Akeneo\Pim\Structure\Component\AttributeTypes” (File: src/Akeneo/Pim/Structure/Component/AttributeTypes.php).

<?php

namespace Akeneo\Pim\Structure\Component;

/**
 * Attribute types dictionary
 *
 * @author    Willy Mesnage <willy.mesnage@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
final class AttributeTypes
{
    const BOOLEAN = 'pim_catalog_boolean';
    const DATE = 'pim_catalog_date';
    const FILE = 'pim_catalog_file';
    const IDENTIFIER = 'pim_catalog_identifier';
    const IMAGE = 'pim_catalog_image';
    const METRIC = 'pim_catalog_metric';
    const NUMBER = 'pim_catalog_number';
    const OPTION_MULTI_SELECT = 'pim_catalog_multiselect';
    const OPTION_SIMPLE_SELECT = 'pim_catalog_simpleselect';
    const PRICE_COLLECTION = 'pim_catalog_price_collection';
    const TEXTAREA = 'pim_catalog_textarea';
    const TEXT = 'pim_catalog_text';
    const REFERENCE_DATA_MULTI_SELECT = 'pim_reference_data_multiselect';
    const REFERENCE_DATA_SIMPLE_SELECT = 'pim_reference_data_simpleselect';
    const REFERENCE_ENTITY_SIMPLE_SELECT = 'akeneo_reference_entity';

    const BACKEND_TYPE_BOOLEAN = 'boolean';
    const BACKEND_TYPE_COLLECTION = 'collections';
    const BACKEND_TYPE_DATE = 'date';
    const BACKEND_TYPE_DATETIME = 'datetime';
    const BACKEND_TYPE_DECIMAL = 'decimal';
    const BACKEND_TYPE_ENTITY = 'entity';
    const BACKEND_TYPE_INTEGER = 'integer';
    const BACKEND_TYPE_MEDIA = 'media';
    const BACKEND_TYPE_METRIC = 'metric';
    const BACKEND_TYPE_OPTION = 'option';
    const BACKEND_TYPE_OPTIONS = 'options';
    const BACKEND_TYPE_PRICE = 'prices';
    const BACKEND_TYPE_REF_DATA_OPTION = 'reference_data_option';
    const BACKEND_TYPE_REF_DATA_OPTIONS = 'reference_data_options';
    const BACKEND_TYPE_TEXTAREA = 'textarea';
    const BACKEND_TYPE_TEXT = 'text';
}

In you own code you could (and should) reference these types instead of using strings.  For example lets say you want to delete attributes by an array of attribute codes but you want to skip identifier attributes from deletion.

<?php

$attributeCodes = [
    'code_a',
    'code_b',
    'code_c',
    'sku',
    'code_d',
    'code_e',
];

$deathCandidates = $this->getContainer()
                    ->get('pim_catalog.repository.attribute')
                    ->findBy(['code' => $attributeCodes]);

$output->writeln("<info>Will delete these attribute(s):</info>");

foreach ($deathCandidates as $item) {
    if($item->getType() == \Akeneo\Pim\Structure\Component\AttributeTypes::IDENTIFIER){
        continue;
    }
    $output->writeln(print_r($item->getCode(), true));
    $this->getContainer()->get('pim_catalog.remover.attribute')->remove($item);
}

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.