- π³π¬Nigeria chike Nigeria
I just encountered this issue with an image field using Drupal 9.5.11. field_config_cardinality settings on the image field are ignored by the site.
- πΊπ¦Ukraine zviryatko
Added next widget that replaces original one.
<?php /** * Add field_config_cardinality support to image widget. */ class CardinalityImageWidget extends ImageWidget { /** * Gets the cardinality of the field. * * @return int * The cardinality of the field. */ public function getCardinality(): int { if ( $this->fieldDefinition instanceof ThirdPartySettingsInterface ) { return (int) $this->fieldDefinition->getThirdPartySetting('field_config_cardinality', 'cardinality_config'); } return $this->fieldDefinition->getFieldStorageDefinition() ->getCardinality(); } /** * {@inheritdoc} */ protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) { $elements = parent::formMultipleElements($items, $form, $form_state); $cardinality = $this->getCardinality(); $elements["#file_upload_description"]["#cardinality"] = $cardinality; if (count(Element::children($elements)) > $cardinality) { $elements[$elements["#file_upload_delta"]]['#access'] = FALSE; } $elements[$elements["#file_upload_delta"]]["#cardinality"] = $cardinality; if ($cardinality === 1) { $elements[$elements["#file_upload_delta"]]["#multiple"] = FALSE; } return $elements; } } ?>
and alter original:
<?php /** * Implements hook_field_widget_info_alter(). */ function custom_module_field_widget_info_alter(array &$info) { if (isset($info['image_image'])) { $info['image_image']['class'] = CardinalityImageWidget::class; } } ?>
The tricky thing is that ImageWidget and FileWidget has complex form that depends on cardinality, so there are now easy way to change it.