BrightcoveCustomField.php in Brightcove Video Connect 3.x
File
src/Entity/BrightcoveCustomField.php
View source
<?php
namespace Drupal\brightcove\Entity;
use Brightcove\Item\CustomField;
use Drupal\brightcove\BrightcoveCustomFieldInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
class BrightcoveCustomField extends BrightcoveCmsEntity implements BrightcoveCustomFieldInterface {
const TYPE_STRING = 'string';
const TYPE_ENUM = 'enum';
public function getCustomFieldId() {
return $this
->get('custom_field_id')->value;
}
public function setCustomFieldId($custom_field_id) {
return $this
->set('custom_field_id', $custom_field_id);
}
public function getEnumValues() {
return $this
->get('enum_values')
->getValue();
}
public function setEnumValues(array $enum_values) {
return $this
->set('enum_values', $enum_values);
}
public function isRequired() {
return $this
->get('required')->value;
}
public function setRequired($required) {
return $this
->set('required', $required);
}
public function getType() {
return $this
->get('type')->value;
}
public function setType($type) {
if (!in_array($type, [
self::TYPE_STRING,
self::TYPE_ENUM,
])) {
throw new \Exception(t('Invalid field type.'));
}
return $this
->set('type', $type);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['bccfid'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The Drupal entity ID of the Brightcove Custom Field.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The Brightcove Custom Field UUID.'))
->setReadOnly(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Custom Field name'))
->setDescription(t('The name of the Brightcove Custom Field.'));
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code for the Brightcove Custom Field.'));
$fields['api_client'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('API Client'))
->setDescription(t('API Client where the Custom Field belongs.'))
->setRequired(TRUE)
->setSetting('target_type', 'brightcove_api_client');
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The username of the Brightcove Custom Field author.'))
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\\brightcove\\Entity\\BrightcoveCustomField::getCurrentUserId')
->setTranslatable(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the Brightcove Custom Field was created.'))
->setTranslatable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the Brightcove Custom Field was last edited.'))
->setTranslatable(TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setRequired(TRUE)
->setDefaultValue(TRUE)
->setSettings([
'on_label' => t('Active'),
'off_label' => t('Inactive'),
]);
$fields['custom_field_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Custom Field ID'))
->setDescription(t('Unique Custom Field ID assigned by Brightcove.'))
->setReadOnly(TRUE);
$fields['description'] = BaseFieldDefinition::create('string')
->setLabel(t('Description'));
$fields['enum_values'] = BaseFieldDefinition::create('string')
->setLabel(t('Enum values'))
->setDescription(t('Max 150 enum value per custom field'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$fields['required'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Required'))
->setDefaultValue(FALSE);
$fields['type'] = BaseFieldDefinition::create('string')
->setLabel(t('Type'));
return $fields;
}
public static function createOrUpdate(CustomField $custom_field, EntityStorageInterface $storage, $api_client_id = NULL) {
$existing_custom_field = $storage
->getQuery()
->condition('custom_field_id', $custom_field
->getId())
->condition('api_client', $api_client_id)
->execute();
if (!empty($existing_custom_field)) {
$custom_field_entity = self::load(reset($existing_custom_field));
}
else {
if (is_null($api_client_id)) {
throw new \Exception(t('To create a new BrightcoveCustomField entity, the api_client_id must be given.'));
}
$values = [
'custom_field_id' => $custom_field
->getId(),
'api_client' => [
'target_id' => $api_client_id,
],
];
$custom_field_entity = self::create($values);
}
if ($custom_field_entity
->getName() != ($enum_values = $custom_field
->getDisplayName())) {
$custom_field_entity
->setName($enum_values);
}
if ($custom_field_entity
->getDescription() != ($enum_values = $custom_field
->getDescription())) {
$custom_field_entity
->setDescription($enum_values);
}
$enum_values = [];
foreach ($custom_field_entity
->getEnumValues() as $enum_value) {
$enum_values[] = $enum_value['value'];
}
if (!empty(array_diff($enum_values, $custom_field_enum_values = $custom_field
->getEnumValues() ?: [])) || !empty(array_diff($custom_field_enum_values, $enum_values))) {
$custom_field_entity
->setEnumValues($custom_field_enum_values);
}
if ($custom_field_entity
->isRequired() != ($required = $custom_field
->isRequired())) {
$custom_field_entity
->setRequired($required);
}
if ($custom_field_entity
->getType() != ($enum_values = $custom_field
->getType())) {
$custom_field_entity
->setType($enum_values);
}
$custom_field_entity
->save();
}
}