Steps to reproduce

1. Install a fresh Drupal 8 site.
2. Edit the image field in the Article.
3. Go to Field settings and set "Allowed number of values" to Unlimited:
1

4. Set up the field in Allowed number of values (Cardinality Instance) to Limited:1
2

5. Create an Article, you will able to add Unlimited images.

CommentFileSizeAuthor
Selection_014.png23.54 KBrpayanm
Selection_013.png8.42 KBrpayanm
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rpayanm created an issue. See original summary.

rpayanm’s picture

I could fix it with this code:

/**
 * Implements hook_field_widget_multivalue_form_alter().
 */
function HOOK_field_widget_multivalue_form_alter(&$elements, FormStateInterface $form_state, $context) {
  if (isset($elements["#attributes"]["data-fcc"]) && $elements["#attributes"]["data-fcc"] > 0) {
    $n = $elements["#attributes"]["data-fcc"];

    if (isset($elements[$n])) {
      unset($elements[$n]);
    }
  }
}
devkinetic’s picture

Do you have any more information as to why this is happening?

chike’s picture

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.

zviryatko’s picture

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.

smustgrave’s picture

Version: 8.x-2.x-dev » 4.0.x-dev
Category: Bug report » Feature request
Priority: Critical » Normal
Status: Active » Postponed (maintainer needs more info)

Can you confirm is still experiencing this?

er.garg.karan made their first commit to this issue’s fork.

er.garg.karan’s picture

Version: 4.0.x-dev » 4.0.0
Category: Feature request » Bug report
Priority: Normal » Major
Status: Postponed (maintainer needs more info) » Needs review

I confirm that this issue is still there even in 4.0.0 version.

@zviryatko, I am using your provided code. But it right now throws a lot of warnings because your code works even for single image fields too. We need it to work only for multiple image fields.

tim-diels’s picture

@er.garg.karan , you opened a MR for 4.0.x but the branch is still 3.0.x. Can you please update that? I'm not able to do it for you...

tim-diels’s picture

Version: 4.0.0 » 4.0.x-dev
Priority: Major » Normal
Status: Needs review » Needs work

smustgrave’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.