Completed
Pull Request — 8.x-1.x (#9)
by Vijay
02:08
created

GalleryMediaBundleConstraintValidator::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 12
rs 9.2
1
<?php
2
3
namespace Drupal\media\Plugin\Validation\Constraint;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
8
/**
9
 * Validates the GalleryMediaBundle constraint.
10
 */
11
class GalleryMediaBundleConstraintValidator extends ConstraintValidator {
12
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function validate($value, Constraint $constraint) {
17
    if (!isset($value)) {
18
      return;
19
    }
20
    $slideshowItems = $value->get($constraint->sourceFieldName);
21
    foreach ($slideshowItems as $item) {
22
      $itemBundle = \Drupal::entityTypeManager()->getStorage('media')->load($item->target_id)->bundle();
23
      if ($itemBundle == "gallery") {
24
        $this->context->addViolation($constraint->message);
25
      }
26
    }
27
  }
28
29
}
30