You are here

class BrightcoveVideoByApiClientConstraintValidator in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Validation/Constraint/BrightcoveVideoByApiClientConstraintValidator.php \Drupal\brightcove\Plugin\Validation\Constraint\BrightcoveVideoByApiClientConstraintValidator
  2. 3.x src/Plugin/Validation/Constraint/BrightcoveVideoByApiClientConstraintValidator.php \Drupal\brightcove\Plugin\Validation\Constraint\BrightcoveVideoByApiClientConstraintValidator

Validates BrightcoveVideo reference by API client.

Hierarchy

Expanded class hierarchy of BrightcoveVideoByApiClientConstraintValidator

File

src/Plugin/Validation/Constraint/BrightcoveVideoByApiClientConstraintValidator.php, line 11

Namespace

Drupal\brightcove\Plugin\Validation\Constraint
View source
class BrightcoveVideoByApiClientConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {

    /* @var \Drupal\Core\Field\EntityReferenceFieldItemList $videos */

    /* @var \Drupal\brightcove\Plugin\Validation\Constraint\BrightcoveVideoByApiClientConstraint $constraint */

    // Get API client.
    $api_client = $value
      ->get('api_client')
      ->getValue();
    if (!empty($api_client[0]['target_id'])) {

      // Get the videos field settings.
      $videos = $value
        ->get('videos');

      /* @var \Drupal\Core\Field\TypedData\FieldItemDataDefinition $item_definitions */
      $item_definitions = $videos
        ->getItemDefinition();
      $settings = $item_definitions
        ->getSettings();

      // Set the required view argument for the videos field in order to be able
      // to validate the field properly.
      $settings['handler_settings']['view']['arguments'] = [
        $api_client[0]['target_id'],
      ];
      $item_definitions
        ->setSettings($settings);
    }
    else {

      // Set violation if the API client is missing.
      $this->context
        ->addViolation($constraint->missingApiClient);
    }
  }

}

Members