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

GalleryMediaBundleConstraintValidator::validate()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 9
c 2
b 1
f 0
nc 5
nop 2
dl 0
loc 14
rs 8.8571
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
    if ($value->hasField($constraint->sourceFieldName)) {
21
      $slideshowItems = $value->get($constraint->sourceFieldName);
22
      foreach ($slideshowItems as $item) {
23
        $itemBundle = \Drupal::entityTypeManager()->getStorage('media')->load($item->target_id)->bundle();
24
        if ($itemBundle == "gallery") {
25
          $this->context->addViolation($constraint->message);
26
        }
27
      }
28
    }
29
  }
30
31
}
32