pm-4.1.x-dev/src/PmContentEntityBase.php

src/PmContentEntityBase.php
<?php

namespace Drupal\pm;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\user\EntityOwnerTrait;

/**
 * Base definition for Content Entities.
 */
abstract class PmContentEntityBase extends ContentEntityBase implements PmContentEntityBaseInterface {

  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);
    // Core info fields.
    // 'label'.
    $fields['label'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Label'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -10,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -10,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['summary'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Summary'))
      ->setDescription(t('A short description in plain text.'))
      ->setDefaultValue('')
      ->setRequired(FALSE)
      ->setDisplayOptions('view', [
        'label' => 'visible',
        'type' => 'basic_string',
        'weight' => 5,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textarea',
        'weight' => 5,
        'settings' => ['rows' => 4],
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);

    // 'uid'.
    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Creator'))
      ->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);
    // 'created'.
    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created on'))
      ->setDescription(t('The time that the Story was created.'))
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'timestamp',
        'weight' => 20,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
        'type' => 'datetime_timestamp',
        'weight' => 20,
      ])
      ->setDisplayConfigurable('view', TRUE);
    // 'changed'.
    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the Story was last edited.'));
    $fields['pm_is_done'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Done'))
      ->setDescription(t('A boolean indicating whether the entity is marked as "done".'))
      ->setDefaultValue(FALSE);
    $base_fields = self::getPmContentEntityBaseFieldDefinitions();
    return array_merge($fields, $base_fields);
  }

  /**
   * Helper method to get list of applicable base fields for the entity.
   *
   * @see getPmContentEntityBaseFieldNames()
   */
  public static function getPmContentEntityBaseFieldDefinitions() {
    $definitions = [];
    $definitions['pm_key'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Key'))
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -10,
      ])
      ->setReadOnly(TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setConstraints([
        'UniqueField' => [],
      ]);
    // User references - members, assignee, reviewer.
    $definitions['pm_member_list'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Members'))
      ->setRequired(FALSE)
      ->setSetting('target_type', 'user')
      ->setDefaultValueCallback(static::class . '::getDefaultEntityOwner')
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'settings' => [
          'match_operator' => 'CONTAINS',
          'size' => 60,
          'placeholder' => '',
        ],
        'weight' => 17,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'weight' => 15,
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $definitions['pm_assignee'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Assignee'))
      ->setRequired(FALSE)
      ->setSetting('target_type', 'user')
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'settings' => [
          'match_operator' => 'CONTAINS',
          'size' => 60,
          'placeholder' => '',
        ],
        'weight' => -9,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'weight' => -9,
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setCardinality(1);
    $definitions['pm_reviewer'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Reviewer'))
      ->setRequired(FALSE)
      ->setSetting('target_type', 'user')
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'settings' => [
          'match_operator' => 'CONTAINS',
          'size' => 60,
          'placeholder' => '',
        ],
        'weight' => -8,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'weight' => -8,
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setCardinality(1);

    // Custom properties - priority, status, date.
    $definitions['pm_priority'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Priority'))
      ->setRequired(FALSE)
      ->setSetting('target_type', 'pm_priority')
      ->setSetting('handler', 'default')
      ->setTranslatable(FALSE)
      ->setDisplayOptions('form', [
        'type' => 'options_select',
        'weight' => 3,
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE)
      ->setCardinality(1);
    $definitions['pm_status'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Status'))
      ->setRequired(FALSE)
      ->setSetting('target_type', 'pm_status')
      ->setSetting('handler', 'default')
      ->setTranslatable(FALSE)
      ->setDisplayOptions('form', [
        'type' => 'options_select',
        'weight' => 2,
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE)
      ->setCardinality(1);
    $definitions['pm_date'] = BaseFieldDefinition::create('daterange')
      ->setLabel(t('Date'))
      ->setRequired(FALSE)
      ->setSettings([
        'datetime_type' => 'date',
      ])
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -4,
      ])
      ->setDisplayOptions('form', [
        'type' => 'daterange_default',
        'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
    // Custom references.
    $map = [
      'pm_project' => [
        'required' => TRUE,
        'label' => [
          'singular' => t('Project'),
          'plural' => t('Project List'),
        ],
        'name' => [
          'singular' => 'Project',
          'plural' => 'Project List',
        ],
      ],
      'pm_organization' => [
        'label' => [
          'singular' => t('Organization'),
          'plural' => t('Organization List'),
        ],
        'name' => [
          'singular' => 'Organization',
          'plural' => 'Organization List',
        ],
      ],
      'pm_board' => [
        'label' => [
          'singular' => t('Board'),
          'plural' => t('Board List'),
        ],
        'name' => [
          'singular' => 'Board',
          'plural' => 'Board List',
        ],
      ],
      'pm_epic' => [
        'label' => [
          'singular' => t('Epic'),
          'plural' => t('Epic List'),
        ],
        'name' => [
          'singular' => 'Epic',
          'plural' => 'Epic List',
        ],
      ],
      'pm_feature' => [
        'label' => [
          'singular' => t('Feature'),
          'plural' => t('Feature List'),
        ],
        'name' => [
          'singular' => 'Feature',
          'plural' => 'Feature List',
        ],
      ],
      'pm_story' => [
        'label' => [
          'singular' => t('Story'),
          'plural' => t('Story List'),
        ],
        'name' => [
          'singular' => 'Story',
          'plural' => 'Story List',
        ],
      ],
      'pm_task' => [
        'label' => [
          'singular' => t('Task'),
          'plural' => t('Task List'),
        ],
        'name' => [
          'singular' => 'Task',
          'plural' => 'Task List',
        ],
      ],
      'pm_sub_task' => [
        'label' => [
          'singular' => t('Sub Task'),
          'plural' => t('Sub Task List'),
        ],
        'name' => [
          'singular' => 'Sub Task',
          'plural' => 'Sub Task List',
        ],
      ],
      'pm_invoice' => [
        'label' => [
          'singular' => t('Invoice'),
          'plural' => t('Invoice List'),
        ],
        'name' => [
          'singular' => 'Invoice',
          'plural' => 'Invoice List',
        ],
      ],
      'pm_note' => [
        'label' => [
          'singular' => t('Note'),
          'plural' => t('Note List'),
        ],
        'name' => [
          'singular' => 'Note',
          'plural' => 'Note List',
        ],
      ],
      'pm_expense' => [
        'label' => [
          'singular' => t('Expense'),
          'plural' => t('Expense List'),
        ],
        'name' => [
          'singular' => 'Expense',
          'plural' => 'Expense List',
        ],
      ],
      'pm_persona' => [
        'label' => [
          'singular' => t('Persona'),
          'plural' => t('Persona List'),
        ],
        'name' => [
          'singular' => 'Persona',
          'plural' => 'Persona List',
        ],
      ],
      'pm_timetracking' => [
        'label' => [
          'singular' => t('Time-tracking'),
          'plural' => t('Time-tracking List'),
        ],
        'name' => [
          'singular' => 'Time-tracking',
          'plural' => 'Time-tracking List',
        ],
      ],
    ];
    $weight = -10;
    foreach ($map as $key => $v) {
      $definitions[$key] = BaseFieldDefinition::create('entity_reference')
        ->setLabel($v['label']['singular'])
        ->setRequired(!empty($v['required']))
        ->setSetting('target_type', $key)
        ->setSetting('handler', 'default')
        ->setDisplayOptions('view', [
          'label' => 'above',
          'weight' => $weight,
        ])
        ->setDisplayOptions('form', [
          'type' => 'entity_reference_autocomplete',
          'weight' => $weight,
          'settings' => [
            'match_operator' => 'CONTAINS',
            'size' => '60',
            'autocomplete_type' => 'tags',
            'placeholder' => '',
          ],
        ])
        ->setDisplayConfigurable('form', TRUE)
        ->setDisplayConfigurable('view', TRUE)
        ->setCardinality(1);
      $weight++;
      $definitions[$key . '_list'] = BaseFieldDefinition::create('entity_reference')
        ->setLabel($v['label']['plural'])
        ->setRequired(FALSE)
        ->setSetting('target_type', $key)
        ->setSetting('handler', 'default')
        ->setDisplayOptions('view', [
          'label' => 'above',
          'weight' => $weight,
        ])
        ->setDisplayOptions('form', [
          'type' => 'entity_reference_autocomplete',
          'weight' => $weight,
          'settings' => [
            'match_operator' => 'CONTAINS',
            'size' => '60',
            'autocomplete_type' => 'tags',
            'placeholder' => '',
          ],
        ])
        ->setDisplayConfigurable('form', TRUE)
        ->setDisplayConfigurable('view', TRUE)
        ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
        ->setConstraints([
          'UniqueField' => [],
        ]);
      $weight++;
    }
    $enabled_fields = static::getPmContentEntityBaseFieldNames();
    foreach ($definitions as $key => $value) {
      if (!in_array($key, $enabled_fields)) {
        unset($definitions[$key]);
      }
    }
    return $definitions;
  }

  /**
   * {@inheritdoc}
   */
  public function isDone(): bool {
    return $this->pm_is_done?->getValue()[0]['value'] ?: FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isOpen(): bool {
    return !$this->isDone();
  }

  /**
   * {@inheritdoc}
   */
  public function setDone() {
    return $this->pm_is_done->setValue(TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function setOpen() {
    return $this->pm_is_done->setValue(FALSE);
  }

  /**
   * {@inheritdoc}
   */
  public function getProject(): ?EntityInterface {
    if ($this->hasField('pm_project')) {
      return $this->pm_project->entity;
    }
    return NULL;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc