You are here

function unique_field_entity_bundle_field_info_alter in Unique field 8

Implements hook_entity_bundle_field_info_alter().

File

./unique_field.module, line 146

Code

function unique_field_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type
    ->id() == 'node') {
    $node_type = \Drupal\node\Entity\NodeType::load($bundle);
    $selected_fields = $node_type
      ->getThirdPartySetting('unique_field', 'type_fields', '');
    $field_scope = $node_type
      ->getThirdPartySetting('unique_field', 'field_scope', '');
    $field_comb = $node_type
      ->getThirdPartySetting('unique_field', 'field_comb', '');
    $show_match = $node_type
      ->getThirdPartySetting('unique_field', 'show_match', '');
    $options = array(
      'bundle' => $bundle,
      'fields' => $selected_fields,
      'scope' => $field_scope,
      'comb' => $field_comb,
      'match' => $show_match,
    );
    foreach ($selected_fields as $key => $field) {
      if (!empty($fields[$field])) {
        $fields[$field]
          ->addConstraint('UniqueField', $options);
      }
    }
  }
}