Create imagefield with maximum number of values programmatically I've created an imagefield in my own module, like this:

$form['heading_img'] = array(
    '#type' =&gt; 'media',
    '#title' =&gt; t('Heading image'),
    '#default_value' =&gt; isset($content['settings']['heading_img']) ? $content['settings']['heading_img'] : '',
    '#theme' =&gt; 'media_widget',
    '#media_options' =&gt; array(
      'global' =&gt; array(
        'types' =&gt; array('image'),
        'schemes' =&gt; array(
          'public' =&gt; 'public',
        ),
        'file_extensions' =&gt; 'png gif jpg jpeg',
        'min_resolution' =&gt; '1920x550',
        'max_filesize' =&gt; '750 KB',
        'uri_scheme' =&gt; '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.
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'] &gt; 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' =&gt; 'media',
    '#title' =&gt; t('Heading image'),
    '#default_value' =&gt; isset($content['settings']['heading_img']) ? $content['settings']['heading_img'] : '',
    '#theme' =&gt; 'media_widget',
    '#media_options' =&gt; array(
      'global' =&gt; array(
        'types' =&gt; array('image'),
        'schemes' =&gt; array(
          'public' =&gt; 'public',
        ),
        'file_extensions' =&gt; 'png gif jpg jpeg',
        'min_resolution' =&gt; '1920x550',
        'max_filesize' =&gt; '750 KB',
        'uri_scheme' =&gt; 'public',
        'multiselect' =&gt; FIELD_CARDINALITY_UNLIMITED
      ),
    ),
);