Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ExternalIdentifierWidget | |
0.00% |
0 / 35 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| defaultSettings | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| settingsForm | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| settingsSummary | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| formElement | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| errorElement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| massageFormValues | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\crm_field\Plugin\Field\FieldWidget; |
| 4 | |
| 5 | use Drupal\Core\Field\FieldItemListInterface; |
| 6 | use Drupal\Core\Field\WidgetBase; |
| 7 | use Drupal\Core\Form\FormStateInterface; |
| 8 | use Symfony\Component\Validator\ConstraintViolationInterface; |
| 9 | |
| 10 | /** |
| 11 | * Defines the 'crm_external_identifier' field widget. |
| 12 | * |
| 13 | * @FieldWidget( |
| 14 | * id = "crm_external_identifier", |
| 15 | * label = @Translation("External Identifier"), |
| 16 | * field_types = {"crm_external_identifier"}, |
| 17 | * ) |
| 18 | */ |
| 19 | class ExternalIdentifierWidget extends WidgetBase { |
| 20 | |
| 21 | /** |
| 22 | * {@inheritdoc} |
| 23 | */ |
| 24 | public static function defaultSettings() { |
| 25 | return ['foo' => 'bar'] + parent::defaultSettings(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * {@inheritdoc} |
| 30 | */ |
| 31 | public function settingsForm(array $form, FormStateInterface $form_state) { |
| 32 | $settings = $this->getSettings(); |
| 33 | $element['foo'] = [ |
| 34 | '#type' => 'textfield', |
| 35 | '#title' => $this->t('Foo'), |
| 36 | '#default_value' => $settings['foo'], |
| 37 | ]; |
| 38 | return $element; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * {@inheritdoc} |
| 43 | */ |
| 44 | public function settingsSummary() { |
| 45 | $settings = $this->getSettings(); |
| 46 | $summary[] = $this->t('Foo: @foo', ['@foo' => $settings['foo']]); |
| 47 | return $summary; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * {@inheritdoc} |
| 52 | */ |
| 53 | public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { |
| 54 | |
| 55 | $element['source'] = [ |
| 56 | '#type' => 'textfield', |
| 57 | '#title' => $this->t('Source'), |
| 58 | '#default_value' => $items[$delta]->source ?? NULL, |
| 59 | '#size' => 20, |
| 60 | ]; |
| 61 | |
| 62 | $element['identifier'] = [ |
| 63 | '#type' => 'textfield', |
| 64 | '#title' => $this->t('Identifier'), |
| 65 | '#default_value' => $items[$delta]->identifier ?? NULL, |
| 66 | '#size' => 20, |
| 67 | ]; |
| 68 | |
| 69 | $element['#theme_wrappers'] = ['container', 'form_element']; |
| 70 | $element['#attributes']['class'][] = 'container-inline'; |
| 71 | $element['#attributes']['class'][] = 'crm-external-identifier-elements'; |
| 72 | $element['#attached']['library'][] = 'crm_field/crm_external_identifier'; |
| 73 | |
| 74 | return $element; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * {@inheritdoc} |
| 79 | */ |
| 80 | public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) { |
| 81 | return isset($violation->arrayPropertyPath[0]) ? $element[$violation->arrayPropertyPath[0]] : $element; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * {@inheritdoc} |
| 86 | */ |
| 87 | public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { |
| 88 | foreach ($values as $delta => $value) { |
| 89 | if ($value['source'] === '') { |
| 90 | $values[$delta]['source'] = NULL; |
| 91 | } |
| 92 | if ($value['identifier'] === '') { |
| 93 | $values[$delta]['identifier'] = NULL; |
| 94 | } |
| 95 | } |
| 96 | return $values; |
| 97 | } |
| 98 | |
| 99 | } |