You are here

function media_browser_plus_taxonomy_term_presave in Media Browser Plus 7.3

Same name and namespace in other branches
  1. 7.2 media_browser_plus.module \media_browser_plus_taxonomy_term_presave()

Implements hook_taxonomy_term_presave().

See also

media_browser_plus_taxonomy_term_update()

File

./media_browser_plus.module, line 536
Media Browser Plus - enhanced file management functions.

Code

function media_browser_plus_taxonomy_term_presave($term) {

  // Avoid running this code when we are auto-creating the root folder term.
  if (empty($term->autocreate)) {

    // Figure out if this is a folder term and if so store the current file path
    // for further processing in media_browser_plus_taxonomy_term_update().
    $vocabulary = taxonomy_vocabulary_machine_name_load('media_folders');
    if (!empty($vocabulary) && $term->vid == $vocabulary->vid) {
      $parent = NULL;
      $root_folder = media_browser_plus_get_media_root_folder();

      // Check if parent handling is necessary.
      if (isset($term->parent)) {

        // Ensure we're dealing with an array.
        if (!is_array($term->parent)) {
          $term->parent = array(
            $term->parent,
          );
        }

        // A folder term can just have one parent.
        if (count($term->parent) > 1) {
          $term->parent = array(
            reset($term->parent),
          );
        }

        // Fetch the used parent.
        $parent = reset($term->parent);
      }

      // A subfolder term is always child of the root folder. Condition ensures
      // we don't interfere while installing the module.
      if ($root_folder && (empty($term->tid) || $term->tid != $root_folder->tid) && empty($parent)) {
        $term->parent = array(
          $root_folder->tid,
        );
      }

      // Actions if this is an existing term.
      if (!empty($term->tid)) {

        // Store current path the check later if the folder was moved.
        $term->media_browser_plus_original_path = media_browser_plus_construct_dir_path($term->original);
      }
    }
  }
}