pm-4.1.x-dev/modules/pm_board/src/Entity/PmBoardColumn.php
modules/pm_board/src/Entity/PmBoardColumn.php
<?php
namespace Drupal\pm_board\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\pm\BundleFieldDefinition;
use Drupal\pm_board\PmBoardColumnInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the Board Column entity class.
*
* @ContentEntityType(
* id = "pm_board_column",
* label = @Translation("Board Column"),
* label_collection = @Translation("Board Columns"),
* label_singular = @Translation("Board Column"),
* label_plural = @Translation("Board Columns"),
* label_count = @PluralTranslation(
* singular = "@count Board Columns",
* plural = "@count Board Columns",
* ),
* bundle_label = @Translation("Board Column type"),
* handlers = {
* "list_builder" = "Drupal\pm_board\PmBoardColumnListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "access" = "Drupal\pm_board\PmBoardColumnAccessControlHandler",
* "form" = {
* "add" = "Drupal\pm_board\Form\PmBoardColumnForm",
* "edit" = "Drupal\pm_board\Form\PmBoardColumnForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* }
* },
* base_table = "pm_board_column",
* admin_permission = "administer pm board column types",
* entity_keys = {
* "id" = "id",
* "bundle" = "bundle",
* "label" = "label",
* "uuid" = "uuid",
* "owner" = "uid",
* },
* links = {
* "collection" = "/admin/pm/board-column",
* "add-form" = "/pm/board-column/add/{pm_board_column_type}",
* "add-page" = "/pm/board-column/add",
* "canonical" = "/pm/board-column/{pm_board_column}",
* "edit-form" = "/pm/board-column/{pm_board_column}/edit",
* "delete-form" = "/pm/board-column/{pm_board_column}/delete",
* },
* bundle_entity_type = "pm_board_column_type",
* field_ui_base_route = "entity.pm_board_column_type.edit_form",
* )
*/
class PmBoardColumn extends ContentEntityBase implements PmBoardColumnInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this->getOwnerId()) {
// If no owner has been set explicitly, make the anonymous user the owner.
$this->setOwnerId(0);
}
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setSetting('target_type', 'user')
->setDefaultValueCallback(static::class . '::getDefaultEntityOwner')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the Board Column was created.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 20,
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the Board Column was last edited.'));
$fields['color'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Color'))
->setDescription(t('Color associated with the column.'))
->setDefaultValue('gray')
->setSettings([
'allowed_values' => [
'blue' => 'Blue',
'gray' => 'Gray',
'red' => 'Red',
'green' => 'Green',
'yellow' => 'Yellow',
'indigo' => 'Indigo',
'purple' => 'Purple',
'pink' => 'Pink',
],
])
->setDisplayOptions('view', [
'label' => 'visible',
'type' => 'list_default',
'weight' => 6,
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 6,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
return $fields;
}
/**
* {@inheritdoc}
*/
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
return self::getBundleSpecificFieldDefinitions($entity_type, $bundle);
}
/**
* Get bundle specific field definitions.
*
* @param string $bundle
* Board Column type name.
*
* @return array
* Array of definition created using BaseFieldDefinition.
*/
public static function getBundleSpecificFieldDefinitions(EntityTypeInterface $entity_type, $bundle) {
$definitions = [];
$definitions[$bundle] = BundleFieldDefinition::create('entity_reference')
// This is essential: the array key is NOT used as the field machine name,
// so it MUST be specified here!
->setName($bundle)
// These two are essential: they define the entity type and the bundle
// that the field is on.
->setTargetEntityTypeId($entity_type->id())
->setTargetBundle($bundle)
->setSetting('target_type', $bundle)
->setLabel(t('Items'))->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setConstraints([
'UniqueField' => [],
]);
return $definitions;
}
}
