It would be great if the Image Block's image upload field could use the improved media file selector widget (assuming the user installed the media module).

Comments

Nitebreed’s picture

For now, you can do this by altering the form like below:

/**
 * Implements hook_form_alter().
 */
function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'imageblock_add_block_form' || $form_id == 'block_admin_configure') {
    if ($form['module']['#value'] == 'imageblock') {
      $form['settings']['imageblock']['#type'] = 'media';
      $form['settings']['imageblock']['#tree'] = TRUE; // Required
      $form['settings']['imageblock']['#description'] = t('Allowed formats: JPG, JPEG, PNG');
      $form['settings']['imageblock']['#media_options'] = array(
        'global' => array(
          'file_extensions' => 'jpg,jpeg,png', // File extensions
          'max_filesize' => '10 MB',
          'file_directory' => 'images', // Will be a subdirectory of the files directory
          'types' => array('image'), // Refers to a file_entity bundle (such as audio, video, image, etc.)
        ),
      );

      // Load block.admin.inc from Block module. Needed for validation
      // in imageblock.module
      form_load_include($form_state, 'inc', 'block', 'block.admin');

      // Define our custom submit handler that save the file selected trough media
      $form['#submit'][] = 'MY_MODULE_form_submit';
    }
  }
}

/**
 * Form submission handler for the forms in MY_MODULE_form_alter().
 */
function MY_MODULE_form_submit($form, &$form_state) {
  // We can update even if it's a new block, because imageblock itself already
  // creates the database entry
  db_update('imageblock')
    ->fields(array(
      'fid' => !empty($form_state['values']['imageblock']) ? $form_state['values']['imageblock'] : 0,
    ))
    ->condition('bid', $form_state['values']['delta'])
    ->execute();
}
Nitebreed’s picture