You are here

function media_browser_plus_submit in Media Browser Plus 7.2

Same name and namespace in other branches
  1. 7 media_browser_plus.module \media_browser_plus_submit()

Submit handler for the media browser forms that create new media entities.

Enhances the media creation process by populating field content for the newly created entities from user-submitted data and/or data available from a remote provider.

See also

media_browser_plus_form_file_entity_add_upload_multiple_alter()

media_browser_plus_form_media_internet_add_alter()

2 string references to 'media_browser_plus_submit'
media_browser_plus_form_file_entity_add_upload_alter in ./media_browser_plus.module
Implements hook_form_FORM_ID_alter() for file_entity_add_upload().
media_browser_plus_form_file_entity_add_upload_multiple_alter in ./media_browser_plus.module
Implements hook_form_FORM_ID_alter() for file_entity_add_upload_multiple().

File

./media_browser_plus.module, line 359
Adds fields to the media browser forms for better UX

Code

function media_browser_plus_submit($form, &$form_state) {

  // Grab the fids of the newly created media entities from the redirect query
  // string that was created by the form's primary submit handler, and load the
  // corresponding entities.
  $fids = NULL;
  if (isset($form_state['files'])) {
    foreach ($form_state['files'] as $i => $fid) {
      $fids[$i] = $fid->fid;
      $i++;
    }
  }
  elseif (isset($form_state['file'])) {
    $fids = $form_state['file']->fid;
  }
  elseif (isset($form_state['redirect'][1]['query']['fid'])) {

    // Compatibility mode.
    // @TODO: Remove once the latest versions of media and file_entity are wide
    // spread.
    $fids = $form_state['redirect'][1]['query']['fid'];
  }
  elseif (isset($form_state['storage']['upload'])) {
    $fids = $form_state['storage']['upload'];
  }
  if (!is_array($fids)) {
    $fids = array(
      $fids,
    );
  }
  $media_entities = file_load_multiple($fids);

  // If tags have been entered, apply them to each new entity.
  if (!empty($form_state['values']['field_tags'])) {

    // Create any new taxonomy terms.
    foreach ($form_state['values']['field_tags'] as $i => &$item) {
      if ($item['tid'] == 'autocreate') {
        $term = (object) $item;
        unset($term->tid);
        taxonomy_term_save($term);
        $item['tid'] = $term->tid;
      }
      unset($item);
    }
    foreach ($media_entities as $media) {
      $media->field_tags[LANGUAGE_NONE] = $form_state['values']['field_tags'];
    }
  }

  // Apply folder.
  foreach ($media_entities as $media) {
    $media->field_folder[LANGUAGE_NONE] = array(
      array(
        'tid' => $form_state['values']['field_folder'],
      ),
    );
  }

  // If the new media is from a 3rd party provider, and that provider also
  // provides MRSS data about the media, then populate the title and description
  // fields from that data.
  if (!empty($form_state['values']['embed_code'])) {
    $provider = media_internet_get_provider($form_state['values']['embed_code']);
    if ($data = _media_browser_plus_metadata($provider)) {
      foreach ($data as $field_name => $value) {
        $field = field_info_field($field_name);

        // Limiting value population only if the field is of type 'text' or
        // 'text_long' isn't as extensible as would be ideal, but we need some
        // protection against populating a field with incompatible content.
        if (isset($field) && in_array($field['type'], array(
          'text',
          'text_long',
        )) && isset($field['bundles']['media'])) {
          foreach ($media_entities as $media) {
            if (in_array($media->type, $field['bundles']['media']) && !isset($media->{$field_name}[LANGUAGE_NONE][0]['value'])) {
              $media->{$field_name}[LANGUAGE_NONE][0]['value'] = $value;
            }
          }
        }
      }
    }
  }
  foreach ($media_entities as $media) {
    media_browser_plus_move_file($form_state['values']['field_folder'], $media);
  }
  if (!empty($form_state['redirect']) && is_string($form_state['redirect']) && mb_strpos($form_state['redirect'], 'file/', 0, 'UTF-8') === 0) {

    // Path was set to file/%file, but we need to thoroughly check if we have
    // access to "admin/content/file", using a failsafe approach.
    $item = menu_get_item('admin/content/file');
    if ($item['access']) {
      $form_state['redirect'] = 'admin/content/file';
    }
  }
}