ProjectTrait.php in Drupal PM (Project Management) 4.x
File
modules/pm_project/src/Entity/ProjectTrait.php
View source
<?php
namespace Drupal\pm_project\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
trait ProjectTrait {
public static function projectFieldDefinitions(EntityTypeInterface $entity_type) {
if (!is_subclass_of($entity_type
->getClass(), ContentEntityInterface::class)) {
throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type
->id() . ' does not implement \\Drupal\\Core\\Entity\\ContentEntityInterface.');
}
$fields = [];
$fields['project'] = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Project'))
->setSetting('target_type', 'pm_project')
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'Project',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE)
->setCardinality(1);
return $fields;
}
public function getProject() {
return (bool) $this
->getEntityKey('project');
}
public function setProject($project_id) {
$this
->set('project', $project_id);
return $this;
}
}
Traits
Name |
Description |
ProjectTrait |
Provides a trait for "belongs" to a project behaviour. |