View source
<?php
namespace Drupal\redhen_connection\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\redhen_connection\ConnectionInterface;
class Connection extends ContentEntityBase implements ConnectionInterface {
use EntityChangedTrait;
use StringTranslationTrait;
public function label() {
$label_pattern = $this->type->entity
->get('connection_label_pattern');
return $this
->t($label_pattern, [
'@label1' => $this
->get('endpoint_1')->entity ? $this
->get('endpoint_1')->entity
->label() : "[entity 1 not found]",
'@label2' => $this
->get('endpoint_2')->entity ? $this
->get('endpoint_2')->entity
->label() : "[entity 2 not found]",
]);
}
public function getType() {
return $this
->bundle();
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function isActive() {
return (bool) $this
->getEntityKey('status');
}
public function setActive($active) {
$this
->set('status', $active ? REDHEN_CONNECTION_ACTIVE : REDHEN_CONNECTION_INACTIVE);
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
for ($x = 1; $x <= REDHEN_CONNECTION_ENDPOINTS; $x++) {
$default_type = $x & 1 ? 'redhen_contact' : 'redhen_org';
$fields["endpoint_{$x}"] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Endpoint @x', [
'@x' => $x,
]))
->setRequired(TRUE)
->setSetting('target_type', $default_type)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => -1,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
}
$fields['role'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Role'))
->setRequired(TRUE)
->setRevisionable(TRUE)
->setSetting('target_type', 'redhen_connection_role')
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => -1,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Active'))
->setDescription(t('A boolean indicating whether the connection is active.'))
->setDefaultValue(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 16,
])
->setDisplayConfigurable('form', TRUE)
->setRevisionable(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the connection was created.'))
->setRevisionable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the connection was last edited.'))
->setRevisionable(TRUE);
return $fields;
}
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
$connection_type = ConnectionType::load($bundle);
$fields = [];
for ($x = 1; $x <= REDHEN_CONNECTION_ENDPOINTS; $x++) {
$endpoint_type = $connection_type
->getEndpointEntityTypeId($x);
$field = 'endpoint_' . $x;
$fields[$field] = clone $base_field_definitions[$field];
if ($endpoint_type) {
$bundles = $connection_type
->getEndpointBundles($x);
$endpoint_entity = \Drupal::entityTypeManager()
->getDefinition($endpoint_type);
$label = !empty($connection_type
->getEndpointLabel($x)) ? $connection_type
->getEndpointLabel($x) : $endpoint_entity
->getLabel();
$fields[$field]
->setSetting('target_type', $endpoint_type)
->setLabel($label);
if (!empty($connection_type
->getEndpointDescription($x))) {
$fields[$field]
->setDescription($connection_type
->getEndpointDescription($x));
}
if (!empty($bundles)) {
$fields[$field]
->setSetting('handler_settings', [
'target_bundles' => $bundles,
]);
}
}
}
$fields['role'] = clone $base_field_definitions['role'];
$fields['role']
->setSetting('handler_settings', [
'connection_type' => $connection_type
->id(),
]);
return $fields;
}
}