You are here

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;

/**
 * Provides a trait for "belongs" to a project behaviour.
 *
 * @package Drupal\pm_project\Entity
 */
trait ProjectTrait {

  /**
   * Returns an array of base field definitions for publishing status.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type to add the publishing status field to.
   *
   * @return \Drupal\Core\Field\BaseFieldDefinition[]
   *   An array of base field definitions.
   *
   * @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
   *   Thrown when the entity type does not implement EntityPublishedInterface
   *   or if it does not have a "published" entity key.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getProject() {
    return (bool) $this
      ->getEntityKey('project');
  }

  /**
   * {@inheritdoc}
   */
  public function setProject($project_id) {
    $this
      ->set('project', $project_id);
    return $this;
  }

}

Traits

Namesort descending Description
ProjectTrait Provides a trait for "belongs" to a project behaviour.