Hi, I want to use Media file selector for selecting image in my custom form, but I'm getting some errors.

The form definition:

<?php
$form['image'] = array(
  '#type' => 'media',
  '#title' => 'Image',
  '#description' => '', // there is another error without declared description
  '#default_value' => $image, // e.g. $image = array('fid' => 1)
  '#media_options' => array(
    'global' => array(
      'types' => array(
        'image' => 'image',
      ),
      'enabledPlugins' => array(
        'upload' => 'upload',
        'media_default--media_browser_1' => 'media_default--media_browser_1',
      ),
      'schemes' => array(
        'public' => 'public',
      ),
      'file_directory' => 'media',
      'file_extensions' => 'png gif jpg jpeg',
      'max_filesize' => '1 MB',
      'uri_scheme' => 'public',
    ),
  ),
);
?>

Errors after I clicked to "Select media" button:

- Notice: Undefined index: default_row_class in function template_preprocess_views_view_unformatted() (line: 770 in file .../sites/all/modules/views/theme/theme.inc).
- Notice: Undefined index: row_class_special in function template_preprocess_views_view_unformatted() (line: 771 in file .../sites/all/modules/views/theme/theme.inc).
- Notice: Undefined offset: 0 in function template_preprocess_views_view_unformatted() (line: 796 in file .../sites/all/modules/views/theme/theme.inc).
- Warning: implode() [function.implode]: Invalid arguments passed in function template_preprocess_views_view_unformatted() (line: 796 in file .../sites/all/modules/views/theme/theme.inc).

Everything works, but the errors are annoying. So, I tried to fix this in "media/media.views.inc" file. I added this code on line 90 and it is OK now, but where I'm wrong? Is there some documentation for this form type? Thanks.

<?php
if (!isset($vars['view']->style_plugin->options['default_row_class'])) {
  $vars['view']->style_plugin->options['default_row_class'] = TRUE;
}
if (!isset($vars['view']->style_plugin->options['row_class_special'])) {
  $vars['view']->style_plugin->options['row_class_special'] = TRUE;
}
?>

Comments

Dave Reid’s picture

Status: Active » Closed (duplicate)

Update your Views to the latest 7.x-2.x-dev. It was a bug there that causes these errors.

kyberman’s picture

Great, it works now. Thank you.

wirka’s picture

This code works well for me with Media 7.x-1.x:

$form['image'] = array(
	'#type' => 'media',		
	'#tree' => TRUE,
	'#title' => t('Image'),
	'#description' => t('Image'),
	'#input' => TRUE,
	'#media_options' => array(
	  'global' => array(
		'types' => array('image'), // Example: array('image', 'audio');
		'schemes' => array('http'), // Example: array('http', 'ftp', 'flickr');
	  ),
	),
	'#default_value' => array('fid' => ''), // Change to your default value
);

This '#tree' => TRUE needs if you have more than one media selectors in one form.

subhojit777’s picture

Another example:

  $form['file'] = array(
    '#type' => 'media',
    '#title' => t('Choose a file'),
    '#description' => t('Choose a file'),
    '#tree' => TRUE,
    '#input' => TRUE,
    '#media_options' => array(
      'global' => array(
        'types' => array(
          'image' => 'image',
          'video' => 'video',
          'audio' => 'audio',
          'document' => 'document',
        ),
        'schemes' => array(
          'public' => 'public',
        ),
        'enabledPlugins' => array(
          'upload' => 'upload',
        ),
      ),
    ),
  );