You are here

function media_browser_plus_get_media_root_folder in Media Browser Plus 7

Same name and namespace in other branches
  1. 7.3 media_browser_plus.module \media_browser_plus_get_media_root_folder()
  2. 7.2 media_browser_plus.module \media_browser_plus_get_media_root_folder()

Loads and (if $autocreate is set) creates the default media folder object.

2 calls to media_browser_plus_get_media_root_folder()
media_browser_plus_enable in ./media_browser_plus.install
Implements hook_enable().
media_browser_plus_field_attach_presave in ./media_browser_plus.module
Implements hook_field_attach_presave().

File

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

Code

function media_browser_plus_get_media_root_folder($autocreate = FALSE) {
  $vocabulary = taxonomy_vocabulary_machine_name_load('media_folders');
  if ($vocabulary) {

    // @todo Would an EntityFieldQuery be appropriate?
    // http://drupal.org/node/916776
    $results = db_query('SELECT * FROM {taxonomy_term_data} ttd WHERE ttd.name = :name AND ttd.vid = :vocabulary', array(
      'name' => 'Media Root',
      'vocabulary' => $vocabulary->vid,
    ));
    $term = NULL;
    foreach ($results as $result) {
      $term_id = $result->tid;
    }
    if (!isset($term_id) && $autocreate) {
      $media_root_term = new stdClass();
      $media_root_term->name = 'Media Root';
      $media_root_term->description = 'default media folder';
      $media_root_term->vid = $vocabulary->vid;
      $media_root_term->weight = '-10';

      // Save (default folder) term.
      taxonomy_term_save($media_root_term);

      // @todo Would an EntityFieldQuery be appropriate?
      // http://drupal.org/node/916776
      $results = db_query('SELECT * FROM {taxonomy_term_data} ttd WHERE ttd.name = :name AND ttd.vid = :vocabulary', array(
        'name' => 'Media Root',
        'vocabulary' => $vocabulary->vid,
      ));
      foreach ($results as $result) {
        $term_id = $result->tid;
      }
    }
    if ($term_id) {
      return media_browser_plus_folder_load($term_id);
    }
  }
}