diff --git a/unique_field.module b/unique_field.module index 3203547..bec08e6 100644 --- a/unique_field.module +++ b/unique_field.module @@ -145,21 +145,24 @@ function unique_field_form_node_type_form_builder($entity_type, NodeTypeInterfac */ 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); + if ($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, + ); + if (!empty($selected_fields)) { + foreach ($selected_fields as $key => $field) { + if(!empty($fields[$field])) { + $fields[$field]->addConstraint('UniqueField', $options); + } + } } } }