Completed
Pull Request — 8.x-1.x (#117)
by
unknown
03:25
created

Cardinality   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 13
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 8 1
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\entity_browser\Plugin\EntityBrowser\WidgetValidation\Cardinality.
6
 */
7
8
namespace Drupal\entity_browser\Plugin\EntityBrowser\WidgetValidation;
9
10
use Drupal\Core\TypedData\ListDataDefinition;
11
use Drupal\entity_browser\WidgetValidationBase;
12
13
/**
14
 * Validates that the widget returns the appropriate number of elements.
15
 *
16
 * @EntityBrowserWidgetValidation(
17
 *   id = "cardinality",
18
 *   label = @Translation("Not empty validator"),
19
 *   constraint = "Count"
20
 * )
21
 */
22
class Cardinality extends WidgetValidationBase {
23
  /**
24
   * {@inheritdoc}
25
   */
26
  public function validate(array $entities, $options = []) {
27
    $data_definition = ListDataDefinition::create('integer');
28
    $plugin_definition = $this->getPluginDefinition();
29
    $data_definition->addConstraint($plugin_definition['constraint'], $options);
30
31
    $typed_data = \Drupal::typedDataManager()->create($data_definition, $entities);
32
    return $typed_data->validate();
33
  }
34
}
35