entity_agree-2.0.x-dev/src/Plugin/Field/FieldType/AgreementItem.php
src/Plugin/Field/FieldType/AgreementItem.php
<?php namespace Drupal\entity_agree\Plugin\Field\FieldType; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; /** * Defines the 'entity_agree' field type. * * @FieldType( * id = "entity_agree", * label = @Translation("Agreement"), * description = @Translation("A field identifying an entity as an agreement."), * category = @Translation("Agreement"), * default_widget = "entity_agree_default", * default_formatter = "entity_agree_form", * ) */ class AgreementItem extends FieldItemBase { const ENTITY_AGREE_STATUS_NA = 0; const ENTITY_AGREE_STATUS_ACTIVE = 1; const ENTITY_AGREE_STATUS_INACTIVE = 2; /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('integer') ->setLabel(t('Integer value')) ->setRequired(TRUE); $properties['config'] = DataDefinition::create('string') ->setLabel(t('Serialized configuration')) ->setRequired(TRUE); return $properties; } /** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { return [ 'columns' => [ 'value' => [ 'type' => 'int', 'size' => 'tiny', ], 'config' => [ 'type' => 'blob', 'size' => 'big', 'serialize' => TRUE, ], ], ]; } /** * Gets plugins for an entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * * @return array * Mulitdimensional array. Outer keys are the field name. Values are * plugins, keyed by plugin type. */ public static function getEntityAgreementPlugins(EntityInterface $entity) { if (!$entity instanceof FieldableEntityInterface) { return []; } $data = []; foreach ($entity->getFieldDefinitions() as $fieldDefinition) { if ($fieldDefinition->getType() != 'entity_agree') { continue; } $data[$fieldDefinition->getName()] = static::getFieldItemListAgreementPlugins($entity->get($fieldDefinition->getName())); } return $data; } /** * Gets plugins for a list of agreement field items (a field). * * @param \Drupal\Core\Field\FieldItemListInterface $items * The field items. * * @return \Drupal\entity_agree\AgreementHandlerInterface[] * Plugins, keyed by plugin type. */ public static function getFieldItemListAgreementPlugins(FieldItemListInterface $items) { $item = $items[0]; if (!$item instanceof AgreementItem) { return []; } return static::getFieldItemAgreementPlugins($item); } /** * Gets plugins for an agreement field item (single delta). * * @param \Drupal\entity_agree\Plugin\Field\FieldType\AgreementItem $item * The field item. * * @return \Drupal\entity_agree\AgreementHandlerInterface[] * Plugins, keyed by plugin type. */ public static function getFieldItemAgreementPlugins(AgreementItem $item) { $config = unserialize($item->config, ['allowed_classes' => FALSE]); if (!is_array($config) || empty($config)) { return []; } /** @var \Drupal\entity_agree\AgreementHandlerPluginManager $pm */ $pm = \Drupal::service('plugin.manager.entity_agree_handler'); $data = []; foreach ($config as $plugin_data) { $data[$plugin_data['id']] = $pm->createInstance($plugin_data['id'], $plugin_data['config']); } return $data; } }