Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| crm_case_external_id_entity_bundle_field_info_alter | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| crm_case_external_id_form_field_config_form_alter | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
20 | |||
| crm_case_external_id_form_field_config_form_validate | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Contains crm_case_external_id.module. |
| 6 | */ |
| 7 | |
| 8 | use Drupal\Core\Entity\EntityTypeInterface; |
| 9 | use Drupal\Core\Form\FormStateInterface; |
| 10 | |
| 11 | /** |
| 12 | * Implements hook_entity_bundle_field_info_alter(). |
| 13 | */ |
| 14 | function crm_case_external_id_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { |
| 15 | |
| 16 | if ($entity_type->id() === 'crm_case') { |
| 17 | if (isset($fields['external_id'])) { |
| 18 | // Use the ID as defined in the annotation of the constraint definition. |
| 19 | $fields['external_id']->addConstraint('CrmCaseExternalId', []); |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Implements hook_form_FORM_ID_alter() for 'field_config_edit_form'. |
| 26 | */ |
| 27 | function crm_case_external_id_form_field_config_form_alter(&$form, FormStateInterface $form_state, $form_id) { |
| 28 | $field = $form_state->getFormObject()->getEntity(); |
| 29 | if ($field->getName() !== 'external_id' || $field->getTargetEntityTypeId() !== 'crm_case') { |
| 30 | return; |
| 31 | |
| 32 | } |
| 33 | $crm_case_bundles = \Drupal::entityTypeManager()->getStorage('crm_case_type')->loadMultiple(); |
| 34 | $options = []; |
| 35 | foreach ($crm_case_bundles as $crm_case_bundle) { |
| 36 | $options[$crm_case_bundle->id()] = $crm_case_bundle->label(); |
| 37 | } |
| 38 | |
| 39 | $form['third_party_settings']['external_id'] = [ |
| 40 | '#type' => 'details', |
| 41 | '#title' => t('External Id settings'), |
| 42 | '#open' => TRUE, |
| 43 | '#group' => 'advanced', |
| 44 | '#weight' => 100, |
| 45 | ]; |
| 46 | |
| 47 | $form['third_party_settings']['external_id']['bundles'] = [ |
| 48 | '#type' => 'checkboxes', |
| 49 | '#title' => t('Bundles'), |
| 50 | '#options' => $options, |
| 51 | '#default_value' => $field->getThirdPartySetting('external_id', 'bundles', []), |
| 52 | ]; |
| 53 | $form['third_party_settings']['external_id']['negate'] = [ |
| 54 | '#type' => 'radios', |
| 55 | '#title' => t('Negate'), |
| 56 | '#options' => [ |
| 57 | 0 => t('Include'), |
| 58 | 1 => t('Exclude'), |
| 59 | ], |
| 60 | '#default_value' => $field->getThirdPartySetting('external_id', 'negate', 0), |
| 61 | ]; |
| 62 | |
| 63 | $form['#validate'][] = 'crm_case_external_id_form_field_config_form_validate'; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Validation for 'field_config_edit_form'. |
| 68 | */ |
| 69 | function crm_case_external_id_form_field_config_form_validate(&$form, FormStateInterface $form_state) { |
| 70 | |
| 71 | $third_party_settings = $form_state->getValue('third_party_settings'); |
| 72 | $bundles = $third_party_settings['external_id']['bundles']; |
| 73 | $bundles = array_filter($bundles); |
| 74 | if (empty($bundles)) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | $field = $form_state->getFormObject()->getEntity(); |
| 79 | $bundle = $field->getTargetBundle(); |
| 80 | $in_array = in_array($bundle, $bundles); |
| 81 | $negate = $third_party_settings['external_id']['negate']; |
| 82 | |
| 83 | if ($negate && $in_array) { |
| 84 | $form_state->setErrorByName('third_party_settings][external_id][bundles', t('The bundle %bundle is selected and negate is enabled.', ['%bundle' => $bundle])); |
| 85 | } |
| 86 | elseif (!$negate && !$in_array) { |
| 87 | $form_state->setErrorByName('third_party_settings][external_id][bundles', t('The bundle %bundle is not selected and negate is disabled.', ['%bundle' => $bundle])); |
| 88 | } |
| 89 | |
| 90 | } |