You are here

public static function BrightcoveCustomField::createOrUpdate in Brightcove Video Connect 3.x

Same name and namespace in other branches
  1. 8.2 src/Entity/BrightcoveCustomField.php \Drupal\brightcove\Entity\BrightcoveCustomField::createOrUpdate()
  2. 8 src/Entity/BrightcoveCustomField.php \Drupal\brightcove\Entity\BrightcoveCustomField::createOrUpdate()

Create or update an existing custom field.

Parameters

\Brightcove\Item\CustomField $custom_field: Brightcove Custom Field object.

\Drupal\Core\Entity\EntityStorageInterface $storage: Custom Field Entity storage.

int|null $api_client_id: The ID of the BrightcoveAPIClient entity.

Throws

\Exception If BrightcoveAPIClient ID is missing when a new entity is being created.

1 call to BrightcoveCustomField::createOrUpdate()
BrightcoveCustomFieldQueueWorker::processItem in src/Plugin/QueueWorker/BrightcoveCustomFieldQueueWorker.php
Works on a single queue item.

File

src/Entity/BrightcoveCustomField.php, line 194

Class

BrightcoveCustomField
Defines the Brightcove Custom Field.

Namespace

Drupal\brightcove\Entity

Code

public static function createOrUpdate(CustomField $custom_field, EntityStorageInterface $storage, $api_client_id = NULL) {

  // Try to get an existing custom field.
  $existing_custom_field = $storage
    ->getQuery()
    ->condition('custom_field_id', $custom_field
    ->getId())
    ->condition('api_client', $api_client_id)
    ->execute();

  // Update existing custom field.
  if (!empty($existing_custom_field)) {

    // Load Brightcove Custom Field.

    /** @var \Drupal\brightcove\Entity\BrightcoveCustomField $custom_field_entity */
    $custom_field_entity = self::load(reset($existing_custom_field));
  }
  else {

    // Make sure we got an api client id when a new custom field is being
    // created.
    if (is_null($api_client_id)) {
      throw new \Exception(t('To create a new BrightcoveCustomField entity, the api_client_id must be given.'));
    }

    // Create new Brightcove custom field entity.
    $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();
}