0

I've created an imagefield in my own module, like this:

$form['heading_img'] = array(
    '#type' => 'media',
    '#title' => t('Heading image'),
    '#default_value' => isset($content['settings']['heading_img']) ? $content['settings']['heading_img'] : '',
    '#theme' => 'media_widget',
    '#media_options' => array(
      'global' => array(
        'types' => array('image'),
        'schemes' => array(
          'public' => 'public',
        ),
        'file_extensions' => 'png gif jpg jpeg',
        'min_resolution' => '1920x550',
        'max_filesize' => '750 KB',
        'uri_scheme' => 'public',
      ),
    ),
);

Sometimes I need more heading images. Is it possible to make this field unlimited?

So you could click on a button: Add an other image. Something like this.

1 Answer 1

0

When scanning though the field definition of the media fields, you'll notice the multiselect property on line 118. Which gets the value of $multiselect from line 103:

$multiselect = ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED);

So in your definition you could try adding this multiselect property with an integer value (eg. 5 to allow 0-5 images), or FIELD_CARDINALITY_UNLIMITED.

$form['heading_img'] = array(
    '#type' => 'media',
    '#title' => t('Heading image'),
    '#default_value' => isset($content['settings']['heading_img']) ? $content['settings']['heading_img'] : '',
    '#theme' => 'media_widget',
    '#media_options' => array(
      'global' => array(
        'types' => array('image'),
        'schemes' => array(
          'public' => 'public',
        ),
        'file_extensions' => 'png gif jpg jpeg',
        'min_resolution' => '1920x550',
        'max_filesize' => '750 KB',
        'uri_scheme' => 'public',
        'multiselect' => FIELD_CARDINALITY_UNLIMITED
      ),
    ),
);
3
  • I saw this, but this is only for uploading images. I can't select multiple images in a specific field, just one image.
    – Alter Ego
    Jan 2, 2017 at 18:51
  • Then you must create multiple form fields with the same definition. So you'll end up with $form['heading_img'], $form['heading_img2'], $form['heading_img3'] and so on. You can do this hardcoded, or using AJAX (see the ajax_example in the examples project). But wouldn't it be easier to create a content type or an entityform for this? Jan 2, 2017 at 19:21
  • This is a part of a custom module, I can't create a content type or entityform for this.
    – Alter Ego
    Jan 2, 2017 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.