2007-07-30 20:33:48 +00:00
< ? php
/**
* @ file
* Administrative page callbacks for the taxonomy module .
*/
/**
2007-11-26 19:46:52 +00:00
* Form builder to list and manage vocabularies .
2007-11-28 10:29:21 +00:00
*
2007-11-26 19:46:52 +00:00
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see taxonomy_overview_vocabularies_submit ()
* @ see theme_taxonomy_overview_vocabularies ()
2007-07-30 20:33:48 +00:00
*/
2009-09-18 00:12:48 +00:00
function taxonomy_overview_vocabularies ( $form ) {
2007-07-30 20:33:48 +00:00
$vocabularies = taxonomy_get_vocabularies ();
2009-09-18 00:12:48 +00:00
$form [ '#tree' ] = TRUE ;
2007-07-30 20:33:48 +00:00
foreach ( $vocabularies as $vocabulary ) {
2008-11-05 12:47:23 +00:00
$form [ $vocabulary -> vid ][ '#vocabulary' ] = $vocabulary ;
2008-07-16 21:59:29 +00:00
$form [ $vocabulary -> vid ][ 'name' ] = array ( '#markup' => check_plain ( $vocabulary -> name ));
2010-10-20 01:31:07 +00:00
$form [ $vocabulary -> vid ][ 'weight' ] = array (
'#type' => 'weight' ,
'#title' => t ( 'Weight for @title' , array ( '@title' => $vocabulary -> name )),
'#title_display' => 'invisible' ,
'#delta' => 10 ,
'#default_value' => $vocabulary -> weight ,
);
2010-03-25 11:53:25 +00:00
$form [ $vocabulary -> vid ][ 'edit' ] = array ( '#type' => 'link' , '#title' => t ( 'edit vocabulary' ), '#href' => " admin/structure/taxonomy/ $vocabulary->machine_name /edit " );
$form [ $vocabulary -> vid ][ 'list' ] = array ( '#type' => 'link' , '#title' => t ( 'list terms' ), '#href' => " admin/structure/taxonomy/ $vocabulary->machine_name " );
$form [ $vocabulary -> vid ][ 'add' ] = array ( '#type' => 'link' , '#title' => t ( 'add terms' ), '#href' => " admin/structure/taxonomy/ $vocabulary->machine_name /add " );
2007-11-26 19:46:52 +00:00
}
2007-11-26 20:58:28 +00:00
// Only make this form include a submit button and weight if more than one
// vocabulary exists.
if ( count ( $vocabularies ) > 1 ) {
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
$form [ 'actions' ][ 'submit' ] = array ( '#type' => 'submit' , '#value' => t ( 'Save' ));
2007-11-26 20:58:28 +00:00
}
elseif ( isset ( $vocabulary )) {
unset ( $form [ $vocabulary -> vid ][ 'weight' ]);
}
2007-11-26 19:46:52 +00:00
return $form ;
}
/**
* Submit handler for vocabularies overview . Updates changed vocabulary weights .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_overview_vocabularies ()
2007-11-26 19:46:52 +00:00
*/
function taxonomy_overview_vocabularies_submit ( $form , & $form_state ) {
foreach ( $form_state [ 'values' ] as $vid => $vocabulary ) {
2008-11-05 12:47:23 +00:00
if ( is_numeric ( $vid ) && $form [ $vid ][ '#vocabulary' ] -> weight != $form_state [ 'values' ][ $vid ][ 'weight' ]) {
$form [ $vid ][ '#vocabulary' ] -> weight = $form_state [ 'values' ][ $vid ][ 'weight' ];
taxonomy_vocabulary_save ( $form [ $vid ][ '#vocabulary' ]);
2007-11-26 19:46:52 +00:00
}
}
2010-03-18 06:36:39 +00:00
drupal_set_message ( t ( 'The configuration options have been saved.' ));
2007-11-26 19:46:52 +00:00
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for the vocabulary overview form as a sortable list of vocabularies .
*
* @ param $variables
* An associative array containing :
* - form : A render element representing the form .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_overview_vocabularies ()
2010-04-13 15:23:03 +00:00
* @ ingroup themeable
2007-11-26 19:46:52 +00:00
*/
2009-10-09 01:00:08 +00:00
function theme_taxonomy_overview_vocabularies ( $variables ) {
$form = $variables [ 'form' ];
2007-11-26 19:46:52 +00:00
$rows = array ();
2009-05-24 17:39:35 +00:00
2007-11-26 19:46:52 +00:00
foreach ( element_children ( $form ) as $key ) {
if ( isset ( $form [ $key ][ 'name' ])) {
$vocabulary = & $form [ $key ];
$row = array ();
$row [] = drupal_render ( $vocabulary [ 'name' ]);
2007-11-26 20:58:28 +00:00
if ( isset ( $vocabulary [ 'weight' ])) {
2009-08-22 14:34:23 +00:00
$vocabulary [ 'weight' ][ '#attributes' ][ 'class' ] = array ( 'vocabulary-weight' );
2007-11-26 20:58:28 +00:00
$row [] = drupal_render ( $vocabulary [ 'weight' ]);
}
2007-11-26 19:46:52 +00:00
$row [] = drupal_render ( $vocabulary [ 'edit' ]);
$row [] = drupal_render ( $vocabulary [ 'list' ]);
$row [] = drupal_render ( $vocabulary [ 'add' ]);
2009-08-22 14:34:23 +00:00
$rows [] = array ( 'data' => $row , 'class' => array ( 'draggable' ));
2007-11-26 19:46:52 +00:00
}
2007-07-30 20:33:48 +00:00
}
2009-06-28 16:05:58 +00:00
2009-10-08 07:58:47 +00:00
$header = array ( t ( 'Vocabulary name' ));
2010-04-24 14:49:14 +00:00
if ( isset ( $form [ 'actions' ])) {
2007-11-26 20:58:28 +00:00
$header [] = t ( 'Weight' );
drupal_add_tabledrag ( 'taxonomy' , 'order' , 'sibling' , 'vocabulary-weight' );
}
$header [] = array ( 'data' => t ( 'Operations' ), 'colspan' => '3' );
2010-01-03 00:44:53 +00:00
return theme ( 'table' , array ( 'header' => $header , 'rows' => $rows , 'empty' => t ( 'No vocabularies available. <a href="@link">Add vocabulary</a>.' , array ( '@link' => url ( 'admin/structure/taxonomy/add' ))), 'attributes' => array ( 'id' => 'taxonomy' ))) . drupal_render_children ( $form );
2007-07-30 20:33:48 +00:00
}
/**
2011-05-17 05:37:12 +00:00
* Form builder for the vocabulary editing form .
2007-07-30 20:33:48 +00:00
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see taxonomy_form_vocabulary_submit ()
2011-05-17 05:37:12 +00:00
* @ see taxonomy_form_vocabulary_validate ()
2007-07-30 20:33:48 +00:00
*/
2009-09-18 00:12:48 +00:00
function taxonomy_form_vocabulary ( $form , & $form_state , $edit = array ()) {
2010-06-17 13:44:45 +00:00
// During initial form build, add the entity to the form state for use
// during form building and processing. During a rebuild, use what is in the
// form state.
if ( ! isset ( $form_state [ 'vocabulary' ])) {
$vocabulary = is_object ( $edit ) ? $edit : ( object ) $edit ;
$defaults = array (
'name' => '' ,
'machine_name' => '' ,
'description' => '' ,
'hierarchy' => 0 ,
'weight' => 0 ,
);
foreach ( $defaults as $key => $value ) {
if ( ! isset ( $vocabulary -> $key )) {
$vocabulary -> $key = $value ;
}
}
$form_state [ 'vocabulary' ] = $vocabulary ;
}
else {
$vocabulary = $form_state [ 'vocabulary' ];
}
// @todo Legacy support. Modules are encouraged to access the entity using
// $form_state. Remove in Drupal 8.
$form [ '#vocabulary' ] = $form_state [ 'vocabulary' ];
2008-02-14 18:41:16 +00:00
// Check whether we need a deletion confirmation form.
if ( isset ( $form_state [ 'confirm_delete' ]) && isset ( $form_state [ 'values' ][ 'vid' ])) {
2009-09-18 00:12:48 +00:00
return taxonomy_vocabulary_confirm_delete ( $form , $form_state , $form_state [ 'values' ][ 'vid' ]);
2008-02-14 18:41:16 +00:00
}
2009-02-13 05:42:24 +00:00
$form [ 'name' ] = array (
'#type' => 'textfield' ,
2009-06-28 12:04:56 +00:00
'#title' => t ( 'Name' ),
2010-06-17 13:44:45 +00:00
'#default_value' => $vocabulary -> name ,
2007-07-30 20:33:48 +00:00
'#maxlength' => 255 ,
'#required' => TRUE ,
2009-06-12 13:59:56 +00:00
);
$form [ 'machine_name' ] = array (
2010-10-13 13:43:21 +00:00
'#type' => 'machine_name' ,
2010-06-17 13:44:45 +00:00
'#default_value' => $vocabulary -> machine_name ,
2011-04-10 20:46:08 +00:00
'#maxlength' => 255 ,
2010-10-13 13:43:21 +00:00
'#machine_name' => array (
'exists' => 'taxonomy_vocabulary_machine_name_load' ,
2009-09-05 15:05:05 +00:00
),
2007-07-30 20:33:48 +00:00
);
2010-10-13 05:19:26 +00:00
$form [ 'old_machine_name' ] = array (
'#type' => 'value' ,
'#value' => $vocabulary -> machine_name ,
);
2009-02-13 05:42:24 +00:00
$form [ 'description' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Description' ),
2010-06-17 13:44:45 +00:00
'#default_value' => $vocabulary -> description ,
2007-11-20 13:24:54 +00:00
);
// Set the hierarchy to "multiple parents" by default. This simplifies the
// vocabulary form and standardizes the term form.
2009-02-13 05:42:24 +00:00
$form [ 'hierarchy' ] = array (
'#type' => 'value' ,
2007-11-26 19:46:52 +00:00
'#value' => '0' ,
2007-11-20 13:24:54 +00:00
);
2007-07-30 20:33:48 +00:00
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
$form [ 'actions' ][ 'submit' ] = array ( '#type' => 'submit' , '#value' => t ( 'Save' ));
2010-06-17 13:44:45 +00:00
if ( isset ( $vocabulary -> vid )) {
2010-04-24 14:49:14 +00:00
$form [ 'actions' ][ 'delete' ] = array ( '#type' => 'submit' , '#value' => t ( 'Delete' ));
2010-06-17 13:44:45 +00:00
$form [ 'vid' ] = array ( '#type' => 'value' , '#value' => $vocabulary -> vid );
$form [ 'module' ] = array ( '#type' => 'value' , '#value' => $vocabulary -> module );
2007-07-30 20:33:48 +00:00
}
2011-05-17 05:37:12 +00:00
$form [ '#validate' ][] = 'taxonomy_form_vocabulary_validate' ;
2007-07-30 20:33:48 +00:00
return $form ;
}
/**
2011-05-17 05:37:12 +00:00
* Form validation handler for taxonomy_form_vocabulary () .
*
* Makes sure that the machine name of the vocabulary is not in the
* disallowed list ( names that conflict with menu items , such as 'list'
* and 'add' ) .
*
* @ see taxonomy_form_vocabulary ()
* @ see taxonomy_form_vocabulary_submit ()
*/
function taxonomy_form_vocabulary_validate ( $form , & $form_state ) {
// During the deletion there is no 'machine_name' key
if ( isset ( $form_state [ 'values' ][ 'machine_name' ])) {
// Do not allow machine names to conflict with taxonomy path arguments.
$machine_name = $form_state [ 'values' ][ 'machine_name' ];
$disallowed = array ( 'add' , 'list' );
if ( in_array ( $machine_name , $disallowed )) {
form_set_error ( 'machine_name' , t ( 'The machine-readable name cannot be "add" or "list".' ));
}
}
}
/**
* Form submission handler for taxonomy_form_vocabulary () .
*
* @ see taxonomy_form_vocabulary ()
* @ see taxonomy_form_vocabulary_validate ()
2007-07-30 20:33:48 +00:00
*/
function taxonomy_form_vocabulary_submit ( $form , & $form_state ) {
2011-06-30 06:16:35 +00:00
if ( $form_state [ 'triggering_element' ][ '#value' ] == t ( 'Delete' )) {
2008-02-14 18:41:16 +00:00
// Rebuild the form to confirm vocabulary deletion.
$form_state [ 'rebuild' ] = TRUE ;
$form_state [ 'confirm_delete' ] = TRUE ;
return ;
}
2010-10-13 05:19:26 +00:00
2010-06-17 13:44:45 +00:00
$vocabulary = $form_state [ 'vocabulary' ];
entity_form_submit_build_entity ( 'taxonomy_vocabulary' , $vocabulary , $form , $form_state );
2010-10-13 05:19:26 +00:00
2008-11-05 12:47:23 +00:00
switch ( taxonomy_vocabulary_save ( $vocabulary )) {
2007-07-30 20:33:48 +00:00
case SAVED_NEW :
2008-11-05 12:47:23 +00:00
drupal_set_message ( t ( 'Created new vocabulary %name.' , array ( '%name' => $vocabulary -> name )));
2010-03-25 11:53:25 +00:00
watchdog ( 'taxonomy' , 'Created new vocabulary %name.' , array ( '%name' => $vocabulary -> name ), WATCHDOG_NOTICE , l ( t ( 'edit' ), 'admin/structure/taxonomy/' . $vocabulary -> machine_name . '/edit' ));
2007-07-30 20:33:48 +00:00
break ;
2009-11-08 11:19:02 +00:00
2007-07-30 20:33:48 +00:00
case SAVED_UPDATED :
2008-11-05 12:47:23 +00:00
drupal_set_message ( t ( 'Updated vocabulary %name.' , array ( '%name' => $vocabulary -> name )));
2010-03-25 11:53:25 +00:00
watchdog ( 'taxonomy' , 'Updated vocabulary %name.' , array ( '%name' => $vocabulary -> name ), WATCHDOG_NOTICE , l ( t ( 'edit' ), 'admin/structure/taxonomy/' . $vocabulary -> machine_name . '/edit' ));
2007-07-30 20:33:48 +00:00
break ;
}
2009-12-01 16:03:35 +00:00
$form_state [ 'values' ][ 'vid' ] = $vocabulary -> vid ;
2008-11-05 12:47:23 +00:00
$form_state [ 'vid' ] = $vocabulary -> vid ;
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/taxonomy' ;
2007-07-30 20:33:48 +00:00
}
/**
2007-11-26 19:46:52 +00:00
* Form builder for the taxonomy terms overview .
2007-11-28 10:29:21 +00:00
*
2007-07-30 20:33:48 +00:00
* Display a tree of all the terms in a vocabulary , with options to edit
2007-11-26 19:46:52 +00:00
* each one . The form is made drag and drop by the theme function .
2007-11-28 10:29:21 +00:00
*
2007-11-26 19:46:52 +00:00
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see taxonomy_overview_terms_submit ()
* @ see theme_taxonomy_overview_terms ()
2007-07-30 20:33:48 +00:00
*/
2009-09-18 00:12:48 +00:00
function taxonomy_overview_terms ( $form , & $form_state , $vocabulary ) {
2007-11-26 19:46:52 +00:00
global $pager_page_array , $pager_total , $pager_total_items ;
2007-07-30 20:33:48 +00:00
2007-11-26 19:46:52 +00:00
// Check for confirmation forms.
if ( isset ( $form_state [ 'confirm_reset_alphabetical' ])) {
2009-10-24 04:23:51 +00:00
return taxonomy_vocabulary_confirm_reset_alphabetical ( $form , $form_state , $vocabulary -> vid );
2007-11-26 19:46:52 +00:00
}
2007-07-30 20:33:48 +00:00
2009-09-18 00:12:48 +00:00
$form [ '#vocabulary' ] = $vocabulary ;
$form [ '#tree' ] = TRUE ;
$form [ '#parent_fields' ] = FALSE ;
2007-11-26 19:46:52 +00:00
$page = isset ( $_GET [ 'page' ]) ? $_GET [ 'page' ] : 0 ;
2008-10-06 23:15:32 +00:00
$page_increment = variable_get ( 'taxonomy_terms_per_page_admin' , 100 ); // Number of terms per page.
2007-11-26 19:46:52 +00:00
$page_entries = 0 ; // Elements shown on this page.
$before_entries = 0 ; // Elements at the root level before this page.
$after_entries = 0 ; // Elements at the root level after this page.
$root_entries = 0 ; // Elements at the root level on this page.
// Terms from previous and next pages are shown if the term tree would have
// been cut in the middle. Keep track of how many extra terms we show on each
// page of terms.
2010-01-04 12:04:07 +00:00
$back_step = NULL ;
$forward_step = 0 ;
2007-07-30 20:33:48 +00:00
2007-11-26 19:46:52 +00:00
// An array of the terms to be displayed on this page.
$current_page = array ();
2010-07-31 13:01:50 +00:00
$delta = 0 ;
2009-10-08 07:58:47 +00:00
$term_deltas = array ();
$tree = taxonomy_get_tree ( $vocabulary -> vid );
$term = current ( $tree );
do {
// In case this tree is completely empty.
if ( empty ( $term )) {
break ;
}
2010-07-31 13:01:50 +00:00
$delta ++ ;
2009-10-08 07:58:47 +00:00
// Count entries before the current page.
2010-01-04 12:04:07 +00:00
if ( $page && ( $page * $page_increment ) > $before_entries && ! isset ( $back_step )) {
2009-10-08 07:58:47 +00:00
$before_entries ++ ;
continue ;
}
// Count entries after the current page.
elseif ( $page_entries > $page_increment && isset ( $complete_tree )) {
$after_entries ++ ;
continue ;
2007-07-30 20:33:48 +00:00
}
2007-11-26 19:46:52 +00:00
2009-10-08 07:58:47 +00:00
// Do not let a term start the page that is not at the root.
2010-01-04 12:04:07 +00:00
if ( isset ( $term -> depth ) && ( $term -> depth > 0 ) && ! isset ( $back_step )) {
$back_step = 0 ;
2009-10-08 07:58:47 +00:00
while ( $pterm = prev ( $tree )) {
$before_entries -- ;
2010-01-04 12:04:07 +00:00
$back_step ++ ;
2009-10-08 07:58:47 +00:00
if ( $pterm -> depth == 0 ) {
prev ( $tree );
continue 2 ; // Jump back to the start of the root level parent.
}
2007-11-26 19:46:52 +00:00
}
2009-10-08 07:58:47 +00:00
}
2010-01-04 12:04:07 +00:00
$back_step = isset ( $back_step ) ? $back_step : 0 ;
2009-10-08 07:58:47 +00:00
// Continue rendering the tree until we reach the a new root item.
2010-01-04 12:04:07 +00:00
if ( $page_entries >= $page_increment + $back_step + 1 && $term -> depth == 0 && $root_entries > 1 ) {
2009-10-08 07:58:47 +00:00
$complete_tree = TRUE ;
// This new item at the root level is the first item on the next page.
$after_entries ++ ;
continue ;
}
2010-01-04 12:04:07 +00:00
if ( $page_entries >= $page_increment + $back_step ) {
$forward_step ++ ;
2009-10-08 07:58:47 +00:00
}
2007-11-26 19:46:52 +00:00
2009-10-08 07:58:47 +00:00
// Finally, if we've gotten down this far, we're rendering a term on this page.
$page_entries ++ ;
$term_deltas [ $term -> tid ] = isset ( $term_deltas [ $term -> tid ]) ? $term_deltas [ $term -> tid ] + 1 : 0 ;
$key = 'tid:' . $term -> tid . ':' . $term_deltas [ $term -> tid ];
2007-11-26 19:46:52 +00:00
2009-10-08 07:58:47 +00:00
// Keep track of the first term displayed on this page.
if ( $page_entries == 1 ) {
$form [ '#first_tid' ] = $term -> tid ;
}
// Keep a variable to make sure at least 2 root elements are displayed.
if ( $term -> parents [ 0 ] == 0 ) {
$root_entries ++ ;
}
$current_page [ $key ] = $term ;
} while ( $term = next ( $tree ));
2007-11-26 19:46:52 +00:00
2009-10-08 07:58:47 +00:00
// Because we didn't use a pager query, set the necessary pager variables.
$total_entries = $before_entries + $page_entries + $after_entries ;
$pager_total_items [ 0 ] = $total_entries ;
$pager_page_array [ 0 ] = $page ;
$pager_total [ 0 ] = ceil ( $total_entries / $page_increment );
2007-11-26 19:46:52 +00:00
// If this form was already submitted once, it's probably hit a validation
// error. Ensure the form is rebuilt in the same order as the user submitted.
2009-03-14 20:13:27 +00:00
if ( ! empty ( $form_state [ 'input' ])) {
$order = array_flip ( array_keys ( $form_state [ 'input' ])); // Get the $_POST order.
2007-11-26 19:46:52 +00:00
$current_page = array_merge ( $order , $current_page ); // Update our form with the new order.
foreach ( $current_page as $key => $term ) {
// Verify this is a term for the current page and set at the current depth.
2009-03-14 20:13:27 +00:00
if ( is_array ( $form_state [ 'input' ][ $key ]) && is_numeric ( $form_state [ 'input' ][ $key ][ 'tid' ])) {
$current_page [ $key ] -> depth = $form_state [ 'input' ][ $key ][ 'depth' ];
2007-11-26 19:46:52 +00:00
}
else {
unset ( $current_page [ $key ]);
}
2007-09-19 19:02:57 +00:00
}
2007-11-26 19:46:52 +00:00
}
2007-07-30 20:33:48 +00:00
2007-11-26 19:46:52 +00:00
// Build the actual form.
foreach ( $current_page as $key => $term ) {
// Save the term for the current page so we don't have to load it a second time.
2010-05-06 05:59:31 +00:00
$form [ $key ][ '#term' ] = ( array ) $term ;
2007-11-26 19:46:52 +00:00
if ( isset ( $term -> parents )) {
$form [ $key ][ '#term' ][ 'parent' ] = $term -> parent = $term -> parents [ 0 ];
unset ( $form [ $key ][ '#term' ][ 'parents' ], $term -> parents );
2007-09-19 19:02:57 +00:00
}
2007-07-30 20:33:48 +00:00
2009-11-03 05:27:18 +00:00
$form [ $key ][ 'view' ] = array ( '#type' => 'link' , '#title' => $term -> name , '#href' => " taxonomy/term/ $term->tid " );
2009-10-08 07:58:47 +00:00
if ( $vocabulary -> hierarchy < 2 && count ( $tree ) > 1 ) {
2007-11-26 19:46:52 +00:00
$form [ '#parent_fields' ] = TRUE ;
$form [ $key ][ 'tid' ] = array (
'#type' => 'hidden' ,
'#value' => $term -> tid
);
$form [ $key ][ 'parent' ] = array (
'#type' => 'hidden' ,
// Yes, default_value on a hidden. It needs to be changeable by the javascript.
'#default_value' => $term -> parent ,
);
$form [ $key ][ 'depth' ] = array (
'#type' => 'hidden' ,
// Same as above, the depth is modified by javascript, so it's a default_value.
'#default_value' => $term -> depth ,
);
2010-07-31 13:01:50 +00:00
$form [ $key ][ 'weight' ] = array (
'#type' => 'weight' ,
'#delta' => $delta ,
'#title_display' => 'invisible' ,
'#title' => t ( 'Weight for added term' ),
'#default_value' => $term -> weight ,
);
2007-11-26 19:46:52 +00:00
}
2009-11-03 05:27:18 +00:00
$form [ $key ][ 'edit' ] = array ( '#type' => 'link' , '#title' => t ( 'edit' ), '#href' => 'taxonomy/term/' . $term -> tid . '/edit' , '#options' => array ( 'query' => drupal_get_destination ()));
2007-09-19 19:02:57 +00:00
}
2007-07-30 20:33:48 +00:00
2007-11-26 19:46:52 +00:00
$form [ '#total_entries' ] = $total_entries ;
$form [ '#page_increment' ] = $page_increment ;
$form [ '#page_entries' ] = $page_entries ;
2010-01-04 12:04:07 +00:00
$form [ '#back_step' ] = $back_step ;
$form [ '#forward_step' ] = $forward_step ;
2010-03-25 11:53:25 +00:00
$form [ '#empty_text' ] = t ( 'No terms available. <a href="@link">Add term</a>.' , array ( '@link' => url ( 'admin/structure/taxonomy/' . $vocabulary -> machine_name . '/add' )));
2007-11-26 19:46:52 +00:00
2009-10-08 07:58:47 +00:00
if ( $vocabulary -> hierarchy < 2 && count ( $tree ) > 1 ) {
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' , '#tree' => FALSE );
$form [ 'actions' ][ 'submit' ] = array (
2007-11-26 19:46:52 +00:00
'#type' => 'submit' ,
2007-12-19 10:31:27 +00:00
'#value' => t ( 'Save' )
2007-11-26 19:46:52 +00:00
);
2010-04-24 14:49:14 +00:00
$form [ 'actions' ][ 'reset_alphabetical' ] = array (
2007-11-26 19:46:52 +00:00
'#type' => 'submit' ,
2007-12-19 15:40:43 +00:00
'#value' => t ( 'Reset to alphabetical' )
2007-11-26 19:46:52 +00:00
);
2009-10-15 16:18:46 +00:00
$form_state [ 'redirect' ] = array ( $_GET [ 'q' ], ( isset ( $_GET [ 'page' ]) ? array ( 'query' => array ( 'page' => $_GET [ 'page' ])) : array ()));
2007-07-30 20:33:48 +00:00
}
2007-11-26 19:46:52 +00:00
return $form ;
2007-07-30 20:33:48 +00:00
}
2007-11-26 19:46:52 +00:00
/**
* Submit handler for terms overview form .
2007-11-28 10:29:21 +00:00
*
2007-11-26 19:46:52 +00:00
* Rather than using a textfield or weight field , this form depends entirely
* upon the order of form elements on the page to determine new weights .
2007-11-28 10:29:21 +00:00
*
2007-11-26 19:46:52 +00:00
* Because there might be hundreds or thousands of taxonomy terms that need to
* be ordered , terms are weighted from 0 to the number of terms in the
* vocabulary , rather than the standard - 10 to 10 scale . Numbers are sorted
* lowest to highest , but are not necessarily sequential . Numbers may be skipped
* when a term has children so that reordering is minimal when a child is
* added or removed from a term .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_overview_terms ()
2007-11-26 19:46:52 +00:00
*/
function taxonomy_overview_terms_submit ( $form , & $form_state ) {
2011-06-30 06:16:35 +00:00
if ( $form_state [ 'triggering_element' ][ '#value' ] == t ( 'Reset to alphabetical' )) {
2007-12-19 15:40:43 +00:00
// Execute the reset action.
2007-11-26 19:46:52 +00:00
if ( $form_state [ 'values' ][ 'reset_alphabetical' ] === TRUE ) {
return taxonomy_vocabulary_confirm_reset_alphabetical_submit ( $form , $form_state );
}
2007-12-19 15:40:43 +00:00
// Rebuild the form to confirm the reset action.
2007-11-26 19:46:52 +00:00
$form_state [ 'rebuild' ] = TRUE ;
$form_state [ 'confirm_reset_alphabetical' ] = TRUE ;
return ;
}
2010-07-31 13:01:50 +00:00
// Sort term order based on weight.
uasort ( $form_state [ 'values' ], 'drupal_sort_weight' );
2007-11-26 19:46:52 +00:00
$vocabulary = $form [ '#vocabulary' ];
$hierarchy = 0 ; // Update the current hierarchy type as we go.
$changed_terms = array ();
2008-11-05 12:47:23 +00:00
$tree = taxonomy_get_tree ( $vocabulary -> vid );
2007-11-26 19:46:52 +00:00
if ( empty ( $tree )) {
return ;
}
// Build a list of all terms that need to be updated on previous pages.
$weight = 0 ;
2010-05-06 05:59:31 +00:00
$term = ( array ) $tree [ 0 ];
2007-11-26 19:46:52 +00:00
while ( $term [ 'tid' ] != $form [ '#first_tid' ]) {
if ( $term [ 'parents' ][ 0 ] == 0 && $term [ 'weight' ] != $weight ) {
$term [ 'parent' ] = $term [ 'parents' ][ 0 ];
$term [ 'weight' ] = $weight ;
$changed_terms [ $term [ 'tid' ]] = $term ;
}
$weight ++ ;
$hierarchy = $term [ 'parents' ][ 0 ] != 0 ? 1 : $hierarchy ;
2010-05-06 05:59:31 +00:00
$term = ( array ) $tree [ $weight ];
2007-11-26 19:46:52 +00:00
}
// Renumber the current page weights and assign any new parents.
$level_weights = array ();
foreach ( $form_state [ 'values' ] as $tid => $values ) {
if ( isset ( $form [ $tid ][ '#term' ])) {
$term = $form [ $tid ][ '#term' ];
// Give terms at the root level a weight in sequence with terms on previous pages.
if ( $values [ 'parent' ] == 0 && $term [ 'weight' ] != $weight ) {
$term [ 'weight' ] = $weight ;
$changed_terms [ $term [ 'tid' ]] = $term ;
}
// Terms not at the root level can safely start from 0 because they're all on this page.
elseif ( $values [ 'parent' ] > 0 ) {
$level_weights [ $values [ 'parent' ]] = isset ( $level_weights [ $values [ 'parent' ]]) ? $level_weights [ $values [ 'parent' ]] + 1 : 0 ;
if ( $level_weights [ $values [ 'parent' ]] != $term [ 'weight' ]) {
$term [ 'weight' ] = $level_weights [ $values [ 'parent' ]];
$changed_terms [ $term [ 'tid' ]] = $term ;
}
}
// Update any changed parents.
if ( $values [ 'parent' ] != $term [ 'parent' ]) {
$term [ 'parent' ] = $values [ 'parent' ];
$changed_terms [ $term [ 'tid' ]] = $term ;
}
$hierarchy = $term [ 'parent' ] != 0 ? 1 : $hierarchy ;
$weight ++ ;
}
}
// Build a list of all terms that need to be updated on following pages.
for ( $weight ; $weight < count ( $tree ); $weight ++ ) {
2010-05-06 05:59:31 +00:00
$term = ( array ) $tree [ $weight ];
2007-11-26 19:46:52 +00:00
if ( $term [ 'parents' ][ 0 ] == 0 && $term [ 'weight' ] != $weight ) {
$term [ 'parent' ] = $term [ 'parents' ][ 0 ];
$term [ 'weight' ] = $weight ;
$changed_terms [ $term [ 'tid' ]] = $term ;
}
$hierarchy = $term [ 'parents' ][ 0 ] != 0 ? 1 : $hierarchy ;
}
// Save all updated terms.
2008-11-09 00:58:03 +00:00
foreach ( $changed_terms as $changed ) {
2010-05-06 05:59:31 +00:00
$term = ( object ) $changed ;
2009-11-02 04:39:16 +00:00
// Update term_hierachy and term_data directly since we don't have a
// fully populated term object to save.
db_update ( 'taxonomy_term_hierarchy' )
-> fields ( array ( 'parent' => $term -> parent ))
-> condition ( 'tid' , $term -> tid , '=' )
-> execute ();
db_update ( 'taxonomy_term_data' )
-> fields ( array ( 'weight' => $term -> weight ))
-> condition ( 'tid' , $term -> tid , '=' )
-> execute ();
2007-11-26 19:46:52 +00:00
}
// Update the vocabulary hierarchy to flat or single hierarchy.
2008-11-05 12:47:23 +00:00
if ( $vocabulary -> hierarchy != $hierarchy ) {
$vocabulary -> hierarchy = $hierarchy ;
taxonomy_vocabulary_save ( $vocabulary );
2007-11-26 19:46:52 +00:00
}
2010-03-18 06:36:39 +00:00
drupal_set_message ( t ( 'The configuration options have been saved.' ));
2007-11-26 19:46:52 +00:00
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a terms overview form as a sortable list of terms .
*
* @ param $variables
* An associative array containing :
* - form : A render element representing the form .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_overview_terms ()
2010-04-13 15:23:03 +00:00
* @ ingroup themeable
2007-11-26 19:46:52 +00:00
*/
2009-10-09 01:00:08 +00:00
function theme_taxonomy_overview_terms ( $variables ) {
$form = $variables [ 'form' ];
2007-11-26 19:46:52 +00:00
$page_increment = $form [ '#page_increment' ];
$page_entries = $form [ '#page_entries' ];
2010-01-04 12:04:07 +00:00
$back_step = $form [ '#back_step' ];
$forward_step = $form [ '#forward_step' ];
2007-11-26 19:46:52 +00:00
// Add drag and drop if parent fields are present in the form.
if ( $form [ '#parent_fields' ]) {
drupal_add_tabledrag ( 'taxonomy' , 'match' , 'parent' , 'term-parent' , 'term-parent' , 'term-id' , FALSE );
drupal_add_tabledrag ( 'taxonomy' , 'depth' , 'group' , 'term-depth' , NULL , NULL , FALSE );
2008-04-14 17:48:46 +00:00
drupal_add_js ( drupal_get_path ( 'module' , 'taxonomy' ) . '/taxonomy.js' );
2010-01-04 12:04:07 +00:00
drupal_add_js ( array ( 'taxonomy' => array ( 'backStep' => $back_step , 'forwardStep' => $forward_step )), 'setting' );
2008-04-14 17:48:46 +00:00
drupal_add_css ( drupal_get_path ( 'module' , 'taxonomy' ) . '/taxonomy.css' );
2007-11-26 19:46:52 +00:00
}
2010-07-31 13:01:50 +00:00
drupal_add_tabledrag ( 'taxonomy' , 'order' , 'sibling' , 'term-weight' );
2007-11-26 19:46:52 +00:00
$errors = form_get_errors () != FALSE ? form_get_errors () : array ();
$rows = array ();
foreach ( element_children ( $form ) as $key ) {
if ( isset ( $form [ $key ][ '#term' ])) {
$term = & $form [ $key ];
$row = array ();
2009-10-09 01:00:08 +00:00
$row [] = ( isset ( $term [ '#term' ][ 'depth' ]) && $term [ '#term' ][ 'depth' ] > 0 ? theme ( 'indentation' , array ( 'size' => $term [ '#term' ][ 'depth' ])) : '' ) . drupal_render ( $term [ 'view' ]);
2007-11-26 19:46:52 +00:00
if ( $form [ '#parent_fields' ]) {
2009-08-22 14:34:23 +00:00
$term [ 'tid' ][ '#attributes' ][ 'class' ] = array ( 'term-id' );
$term [ 'parent' ][ '#attributes' ][ 'class' ] = array ( 'term-parent' );
$term [ 'depth' ][ '#attributes' ][ 'class' ] = array ( 'term-depth' );
2007-11-26 19:46:52 +00:00
$row [ 0 ] .= drupal_render ( $term [ 'parent' ]) . drupal_render ( $term [ 'tid' ]) . drupal_render ( $term [ 'depth' ]);
}
2010-07-31 13:01:50 +00:00
$term [ 'weight' ][ '#attributes' ][ 'class' ] = array ( 'term-weight' );
$row [] = drupal_render ( $term [ 'weight' ]);
2007-11-26 19:46:52 +00:00
$row [] = drupal_render ( $term [ 'edit' ]);
$row = array ( 'data' => $row );
$rows [ $key ] = $row ;
}
}
// Add necessary classes to rows.
$row_position = 0 ;
foreach ( $rows as $key => $row ) {
2009-08-22 14:34:23 +00:00
$rows [ $key ][ 'class' ] = array ();
2007-11-26 19:46:52 +00:00
if ( isset ( $form [ '#parent_fields' ])) {
2009-08-22 14:34:23 +00:00
$rows [ $key ][ 'class' ][] = 'draggable' ;
2007-11-26 19:46:52 +00:00
}
// Add classes that mark which terms belong to previous and next pages.
2010-01-04 12:04:07 +00:00
if ( $row_position < $back_step || $row_position >= $page_entries - $forward_step ) {
2009-08-22 14:34:23 +00:00
$rows [ $key ][ 'class' ][] = 'taxonomy-term-preview' ;
2007-11-26 19:46:52 +00:00
}
if ( $row_position !== 0 && $row_position !== count ( $rows ) - 1 ) {
2010-01-04 12:04:07 +00:00
if ( $row_position == $back_step - 1 || $row_position == $page_entries - $forward_step - 1 ) {
2009-08-22 14:34:23 +00:00
$rows [ $key ][ 'class' ][] = 'taxonomy-term-divider-top' ;
2007-11-26 19:46:52 +00:00
}
2010-01-04 12:04:07 +00:00
elseif ( $row_position == $back_step || $row_position == $page_entries - $forward_step ) {
2009-08-22 14:34:23 +00:00
$rows [ $key ][ 'class' ][] = 'taxonomy-term-divider-bottom' ;
2007-11-26 19:46:52 +00:00
}
}
// Add an error class if this row contains a form error.
foreach ( $errors as $error_key => $error ) {
if ( strpos ( $error_key , $key ) === 0 ) {
2009-08-22 14:34:23 +00:00
$rows [ $key ][ 'class' ][] = 'error' ;
2007-11-26 19:46:52 +00:00
}
}
$row_position ++ ;
}
if ( empty ( $rows )) {
2010-09-26 23:24:53 +00:00
$rows [] = array ( array ( 'data' => $form [ '#empty_text' ], 'colspan' => '3' ));
2007-11-26 19:46:52 +00:00
}
2010-07-31 13:01:50 +00:00
$header = array ( t ( 'Name' ), t ( 'Weight' ), t ( 'Operations' ));
2009-10-09 01:00:08 +00:00
$output = theme ( 'table' , array ( 'header' => $header , 'rows' => $rows , 'attributes' => array ( 'id' => 'taxonomy' )));
2009-02-03 18:55:32 +00:00
$output .= drupal_render_children ( $form );
2010-10-06 13:38:40 +00:00
$output .= theme ( 'pager' );
2007-11-26 19:46:52 +00:00
return $output ;
}
2007-09-13 07:54:27 +00:00
2007-07-30 20:33:48 +00:00
/**
* Form function for the term edit form .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see taxonomy_form_term_submit ()
2007-07-30 20:33:48 +00:00
*/
2009-10-15 12:13:45 +00:00
function taxonomy_form_term ( $form , & $form_state , $edit = array (), $vocabulary = NULL ) {
2010-06-17 13:44:45 +00:00
// During initial form build, add the term entity to the form state for use
// during form building and processing. During a rebuild, use what is in the
// form state.
if ( ! isset ( $form_state [ 'term' ])) {
$term = is_object ( $edit ) ? $edit : ( object ) $edit ;
if ( ! isset ( $vocabulary ) && isset ( $term -> vid )) {
$vocabulary = taxonomy_vocabulary_load ( $term -> vid );
}
$defaults = array (
'name' => '' ,
'description' => '' ,
2010-09-28 03:30:37 +00:00
'format' => NULL ,
2010-06-17 13:44:45 +00:00
'vocabulary_machine_name' => isset ( $vocabulary ) ? $vocabulary -> machine_name : NULL ,
'tid' => NULL ,
'weight' => 0 ,
);
foreach ( $defaults as $key => $value ) {
if ( ! isset ( $term -> $key )) {
$term -> $key = $value ;
}
}
$form_state [ 'term' ] = $term ;
}
else {
$term = $form_state [ 'term' ];
if ( ! isset ( $vocabulary ) && isset ( $term -> vid )) {
$vocabulary = taxonomy_vocabulary_load ( $term -> vid );
}
2009-06-12 13:59:56 +00:00
}
2010-06-17 13:44:45 +00:00
$parent = array_keys ( taxonomy_get_parents ( $term -> tid ));
$form [ '#term' ] = ( array ) $term ;
2007-11-26 19:46:52 +00:00
$form [ '#term' ][ 'parent' ] = $parent ;
2008-11-05 12:47:23 +00:00
$form [ '#vocabulary' ] = $vocabulary ;
2007-11-26 19:46:52 +00:00
// Check for confirmation forms.
if ( isset ( $form_state [ 'confirm_delete' ])) {
2010-06-17 13:44:45 +00:00
return array_merge ( $form , taxonomy_term_confirm_delete ( $form , $form_state , $term -> tid ));
2007-11-26 19:46:52 +00:00
}
2010-01-03 00:45:51 +00:00
$form [ 'name' ] = array (
2007-07-30 20:33:48 +00:00
'#type' => 'textfield' ,
2010-01-09 12:54:51 +00:00
'#title' => t ( 'Name' ),
2010-06-17 13:44:45 +00:00
'#default_value' => $term -> name ,
2007-07-30 20:33:48 +00:00
'#maxlength' => 255 ,
2010-02-10 06:28:10 +00:00
'#required' => TRUE ,
'#weight' => - 5 ,
);
2010-01-03 00:45:51 +00:00
$form [ 'description' ] = array (
2010-03-07 23:14:20 +00:00
'#type' => 'text_format' ,
2007-07-30 20:33:48 +00:00
'#title' => t ( 'Description' ),
2010-06-17 13:44:45 +00:00
'#default_value' => $term -> description ,
'#format' => $term -> format ,
2010-02-10 06:28:10 +00:00
'#weight' => 0 ,
2009-11-07 14:18:07 +00:00
);
2007-07-30 20:33:48 +00:00
2009-06-12 13:59:56 +00:00
$form [ 'vocabulary_machine_name' ] = array (
2010-10-20 01:31:07 +00:00
'#type' => 'value' ,
2010-06-17 13:44:45 +00:00
'#value' => isset ( $term -> vocabulary_machine_name ) ? $term -> vocabulary_machine_name : $vocabulary -> name ,
2009-06-12 13:59:56 +00:00
);
2012-06-09 19:51:03 +00:00
$langcode = entity_language ( 'taxonomy_term' , $term );
field_attach_form ( 'taxonomy_term' , $term , $form , $form_state , $langcode );
2009-06-12 13:59:56 +00:00
2010-01-09 12:54:51 +00:00
$form [ 'relations' ] = array (
2007-11-20 13:24:54 +00:00
'#type' => 'fieldset' ,
2010-01-09 12:54:51 +00:00
'#title' => t ( 'Relations' ),
2007-11-20 13:24:54 +00:00
'#collapsible' => TRUE ,
2008-10-29 10:08:52 +00:00
'#collapsed' => $vocabulary -> hierarchy < 2 ,
2010-01-18 03:33:43 +00:00
'#weight' => 10 ,
2007-11-20 13:24:54 +00:00
);
2007-07-30 20:33:48 +00:00
2007-11-23 12:32:21 +00:00
// taxonomy_get_tree and taxonomy_get_parents may contain large numbers of
// items so we check for taxonomy_override_selector before loading the
// full vocabulary. Contrib modules can then intercept before
2007-11-23 13:34:55 +00:00
// hook_form_alter to provide scalable alternatives.
2007-11-23 12:32:21 +00:00
if ( ! variable_get ( 'taxonomy_override_selector' , FALSE )) {
2010-06-17 13:44:45 +00:00
$parent = array_keys ( taxonomy_get_parents ( $term -> tid ));
$children = taxonomy_get_tree ( $vocabulary -> vid , $term -> tid );
2007-07-30 20:33:48 +00:00
2007-11-23 12:32:21 +00:00
// A term can't be the child of itself, nor of its children.
foreach ( $children as $child ) {
$exclude [] = $child -> tid ;
}
2010-06-17 13:44:45 +00:00
$exclude [] = $term -> tid ;
2007-07-30 20:33:48 +00:00
2009-10-14 20:22:04 +00:00
$tree = taxonomy_get_tree ( $vocabulary -> vid );
$options = array ( '<' . t ( 'root' ) . '>' );
2010-04-06 05:44:07 +00:00
if ( empty ( $parent )) {
$parent = array ( 0 );
}
2010-06-17 13:44:45 +00:00
foreach ( $tree as $item ) {
if ( ! in_array ( $item -> tid , $exclude )) {
$options [ $item -> tid ] = str_repeat ( '-' , $item -> depth ) . $item -> name ;
2009-10-14 20:22:04 +00:00
}
}
2010-01-09 12:54:51 +00:00
$form [ 'relations' ][ 'parent' ] = array (
2009-10-14 20:22:04 +00:00
'#type' => 'select' ,
'#title' => t ( 'Parent terms' ),
'#options' => $options ,
'#default_value' => $parent ,
'#multiple' => TRUE ,
);
2009-12-02 14:56:32 +00:00
2007-11-23 12:32:21 +00:00
}
2010-01-09 12:54:51 +00:00
$form [ 'relations' ][ 'weight' ] = array (
2007-11-26 19:46:52 +00:00
'#type' => 'textfield' ,
2007-07-30 20:33:48 +00:00
'#title' => t ( 'Weight' ),
2007-11-26 19:46:52 +00:00
'#size' => 6 ,
2010-06-17 13:44:45 +00:00
'#default_value' => $term -> weight ,
2007-11-26 19:46:52 +00:00
'#description' => t ( 'Terms are displayed in ascending order by weight.' ),
2010-01-18 03:33:43 +00:00
'#required' => TRUE ,
);
2007-07-30 20:33:48 +00:00
$form [ 'vid' ] = array (
'#type' => 'value' ,
2010-01-18 03:33:43 +00:00
'#value' => $vocabulary -> vid ,
);
$form [ 'tid' ] = array (
'#type' => 'value' ,
2010-06-17 13:44:45 +00:00
'#value' => $term -> tid ,
2010-01-18 03:33:43 +00:00
);
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-01-18 03:33:43 +00:00
$form [ 'actions' ][ 'submit' ] = array (
2007-07-30 20:33:48 +00:00
'#type' => 'submit' ,
2010-01-18 03:33:43 +00:00
'#value' => t ( 'Save' ),
'#weight' => 5 ,
);
2007-07-30 20:33:48 +00:00
2010-06-17 13:44:45 +00:00
if ( $term -> tid ) {
2010-01-18 03:33:43 +00:00
$form [ 'actions' ][ 'delete' ] = array (
2007-07-30 20:33:48 +00:00
'#type' => 'submit' ,
2009-10-14 14:14:44 +00:00
'#value' => t ( 'Delete' ),
'#access' => user_access ( " delete terms in $vocabulary->vid " ) || user_access ( 'administer taxonomy' ),
2010-01-18 03:33:43 +00:00
'#weight' => 10 ,
2009-10-14 14:14:44 +00:00
);
2007-07-30 20:33:48 +00:00
}
else {
2009-09-21 06:44:14 +00:00
$form_state [ 'redirect' ] = $_GET [ 'q' ];
2007-07-30 20:33:48 +00:00
}
return $form ;
}
/**
2009-06-12 13:59:56 +00:00
* Validation handler for the term form .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_form_term ()
2007-11-26 19:46:52 +00:00
*/
function taxonomy_form_term_validate ( $form , & $form_state ) {
2010-06-17 13:44:45 +00:00
entity_form_field_validate ( 'taxonomy_term' , $form , $form_state );
2009-06-12 13:59:56 +00:00
// Ensure numeric values.
2007-11-26 19:46:52 +00:00
if ( isset ( $form_state [ 'values' ][ 'weight' ]) && ! is_numeric ( $form_state [ 'values' ][ 'weight' ])) {
form_set_error ( 'weight' , t ( 'Weight value must be numeric.' ));
}
}
/**
* Submit handler to insert or update a term .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_form_term ()
2007-07-30 20:33:48 +00:00
*/
function taxonomy_form_term_submit ( $form , & $form_state ) {
2011-06-30 06:16:35 +00:00
if ( $form_state [ 'triggering_element' ][ '#value' ] == t ( 'Delete' )) {
2007-11-26 19:46:52 +00:00
// Execute the term deletion.
if ( $form_state [ 'values' ][ 'delete' ] === TRUE ) {
return taxonomy_term_confirm_delete_submit ( $form , $form_state );
}
// Rebuild the form to confirm term deletion.
$form_state [ 'rebuild' ] = TRUE ;
$form_state [ 'confirm_delete' ] = TRUE ;
return ;
}
2010-11-20 06:49:47 +00:00
$term = taxonomy_form_term_submit_build_taxonomy_term ( $form , $form_state );
2009-06-12 13:59:56 +00:00
2008-11-05 14:08:11 +00:00
$status = taxonomy_term_save ( $term );
2008-11-02 14:42:45 +00:00
switch ( $status ) {
2007-07-30 20:33:48 +00:00
case SAVED_NEW :
2008-11-05 14:08:11 +00:00
drupal_set_message ( t ( 'Created new term %term.' , array ( '%term' => $term -> name )));
watchdog ( 'taxonomy' , 'Created new term %term.' , array ( '%term' => $term -> name ), WATCHDOG_NOTICE , l ( t ( 'edit' ), 'taxonomy/term/' . $term -> tid . '/edit' ));
2007-07-30 20:33:48 +00:00
break ;
case SAVED_UPDATED :
2008-11-05 14:08:11 +00:00
drupal_set_message ( t ( 'Updated term %term.' , array ( '%term' => $term -> name )));
watchdog ( 'taxonomy' , 'Updated term %term.' , array ( '%term' => $term -> name ), WATCHDOG_NOTICE , l ( t ( 'edit' ), 'taxonomy/term/' . $term -> tid . '/edit' ));
2010-03-28 11:39:35 +00:00
// Clear the page and block caches to avoid stale data.
cache_clear_all ();
2007-07-30 20:33:48 +00:00
break ;
}
2009-10-08 07:58:47 +00:00
$current_parent_count = count ( $form_state [ 'values' ][ 'parent' ]);
$previous_parent_count = count ( $form [ '#term' ][ 'parent' ]);
// Root doesn't count if it's the only parent.
if ( $current_parent_count == 1 && isset ( $form_state [ 'values' ][ 'parent' ][ 0 ])) {
$current_parent_count = 0 ;
$form_state [ 'values' ][ 'parent' ] = array ();
}
2007-11-28 10:29:21 +00:00
2009-10-08 07:58:47 +00:00
// If the number of parents has been reduced to one or none, do a check on the
// parents of every term in the vocabulary value.
if ( $current_parent_count < $previous_parent_count && $current_parent_count < 2 ) {
taxonomy_check_vocabulary_hierarchy ( $form [ '#vocabulary' ], $form_state [ 'values' ]);
}
// If we've increased the number of parents and this is a single or flat
// hierarchy, update the vocabulary immediately.
elseif ( $current_parent_count > $previous_parent_count && $form [ '#vocabulary' ] -> hierarchy < 2 ) {
$form [ '#vocabulary' ] -> hierarchy = $current_parent_count == 1 ? 1 : 2 ;
taxonomy_vocabulary_save ( $form [ '#vocabulary' ]);
2007-11-26 19:46:52 +00:00
}
2009-12-01 16:03:35 +00:00
$form_state [ 'values' ][ 'tid' ] = $term -> tid ;
2008-11-05 14:08:11 +00:00
$form_state [ 'tid' ] = $term -> tid ;
2007-07-30 20:33:48 +00:00
}
2009-06-12 13:59:56 +00:00
/**
2010-06-17 13:44:45 +00:00
* Updates the form state 's term entity by processing this submission' s values .
2009-06-12 13:59:56 +00:00
*/
2010-11-20 06:49:47 +00:00
function taxonomy_form_term_submit_build_taxonomy_term ( $form , & $form_state ) {
2010-06-17 13:44:45 +00:00
$term = $form_state [ 'term' ];
entity_form_submit_build_entity ( 'taxonomy_term' , $term , $form , $form_state );
2010-05-13 07:53:02 +00:00
// Convert text_format field into values expected by taxonomy_term_save().
$description = $form_state [ 'values' ][ 'description' ];
$term -> description = $description [ 'value' ];
$term -> format = $description [ 'format' ];
2009-06-12 13:59:56 +00:00
return $term ;
}
2007-07-30 20:33:48 +00:00
/**
* Form builder for the term delete form .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see taxonomy_term_confirm_delete_submit ()
2007-07-30 20:33:48 +00:00
*/
2009-09-18 00:12:48 +00:00
function taxonomy_term_confirm_delete ( $form , & $form_state , $tid ) {
2008-09-19 20:25:03 +00:00
$term = taxonomy_term_load ( $tid );
2007-07-30 20:33:48 +00:00
2010-02-17 05:42:42 +00:00
// Always provide entity id in the same form key as in the entity edit form.
$form [ 'tid' ] = array ( '#type' => 'value' , '#value' => $tid );
$form [ '#term' ] = $term ;
2007-07-30 20:33:48 +00:00
$form [ 'type' ] = array ( '#type' => 'value' , '#value' => 'term' );
$form [ 'name' ] = array ( '#type' => 'value' , '#value' => $term -> name );
2009-06-12 13:59:56 +00:00
$form [ 'vocabulary_machine_name' ] = array ( '#type' => 'value' , '#value' => $term -> vocabulary_machine_name );
2007-11-26 19:46:52 +00:00
$form [ 'delete' ] = array ( '#type' => 'value' , '#value' => TRUE );
2007-07-30 20:33:48 +00:00
return confirm_form ( $form ,
2010-02-17 05:42:42 +00:00
t ( 'Are you sure you want to delete the term %title?' ,
array ( '%title' => $term -> name )),
'admin/structure/taxonomy' ,
t ( 'Deleting a term will delete all its children if there are any. This action cannot be undone.' ),
t ( 'Delete' ),
t ( 'Cancel' ));
2007-07-30 20:33:48 +00:00
}
2007-11-26 19:46:52 +00:00
/**
* Submit handler to delete a term after confirmation .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_term_confirm_delete ()
2007-11-26 19:46:52 +00:00
*/
2007-07-30 20:33:48 +00:00
function taxonomy_term_confirm_delete_submit ( $form , & $form_state ) {
2009-01-28 01:09:58 +00:00
taxonomy_term_delete ( $form_state [ 'values' ][ 'tid' ]);
2007-11-26 19:46:52 +00:00
taxonomy_check_vocabulary_hierarchy ( $form [ '#vocabulary' ], $form_state [ 'values' ]);
2007-07-30 20:33:48 +00:00
drupal_set_message ( t ( 'Deleted term %name.' , array ( '%name' => $form_state [ 'values' ][ 'name' ])));
watchdog ( 'taxonomy' , 'Deleted term %name.' , array ( '%name' => $form_state [ 'values' ][ 'name' ]), WATCHDOG_NOTICE );
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/taxonomy' ;
2010-11-14 20:42:45 +00:00
cache_clear_all ();
2007-07-30 20:33:48 +00:00
return ;
}
/**
* Form builder for the vocabulary delete confirmation form .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see taxonomy_vocabulary_confirm_delete_submit ()
2007-07-30 20:33:48 +00:00
*/
2009-09-18 00:12:48 +00:00
function taxonomy_vocabulary_confirm_delete ( $form , & $form_state , $vid ) {
2007-07-30 20:33:48 +00:00
$vocabulary = taxonomy_vocabulary_load ( $vid );
2010-02-17 05:42:42 +00:00
// Always provide entity id in the same form key as in the entity edit form.
$form [ 'vid' ] = array ( '#type' => 'value' , '#value' => $vid );
$form [ '#vocabulary' ] = $vocabulary ;
2008-02-14 18:41:16 +00:00
$form [ '#id' ] = 'taxonomy_vocabulary_confirm_delete' ;
2007-07-30 20:33:48 +00:00
$form [ 'type' ] = array ( '#type' => 'value' , '#value' => 'vocabulary' );
$form [ 'name' ] = array ( '#type' => 'value' , '#value' => $vocabulary -> name );
2008-02-14 18:41:16 +00:00
$form [ '#submit' ] = array ( 'taxonomy_vocabulary_confirm_delete_submit' );
2007-07-30 20:33:48 +00:00
return confirm_form ( $form ,
2010-02-17 05:42:42 +00:00
t ( 'Are you sure you want to delete the vocabulary %title?' ,
array ( '%title' => $vocabulary -> name )),
'admin/structure/taxonomy' ,
t ( 'Deleting a vocabulary will delete all the terms in it. This action cannot be undone.' ),
t ( 'Delete' ),
t ( 'Cancel' ));
2007-07-30 20:33:48 +00:00
}
2007-11-26 19:46:52 +00:00
/**
* Submit handler to delete a vocabulary after confirmation .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_vocabulary_confirm_delete ()
2007-11-26 19:46:52 +00:00
*/
2007-07-30 20:33:48 +00:00
function taxonomy_vocabulary_confirm_delete_submit ( $form , & $form_state ) {
2008-11-05 12:47:23 +00:00
$status = taxonomy_vocabulary_delete ( $form_state [ 'values' ][ 'vid' ]);
2007-07-30 20:33:48 +00:00
drupal_set_message ( t ( 'Deleted vocabulary %name.' , array ( '%name' => $form_state [ 'values' ][ 'name' ])));
watchdog ( 'taxonomy' , 'Deleted vocabulary %name.' , array ( '%name' => $form_state [ 'values' ][ 'name' ]), WATCHDOG_NOTICE );
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/taxonomy' ;
2010-11-14 20:42:45 +00:00
cache_clear_all ();
2007-07-30 20:33:48 +00:00
return ;
}
2007-11-26 19:46:52 +00:00
/**
2008-12-30 16:43:20 +00:00
* Form builder to confirm resetting a vocabulary to alphabetical order .
2007-11-28 10:29:21 +00:00
*
2007-11-26 19:46:52 +00:00
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see taxonomy_vocabulary_confirm_reset_alphabetical_submit ()
2007-11-26 19:46:52 +00:00
*/
2009-09-18 00:12:48 +00:00
function taxonomy_vocabulary_confirm_reset_alphabetical ( $form , & $form_state , $vid ) {
2007-11-26 19:46:52 +00:00
$vocabulary = taxonomy_vocabulary_load ( $vid );
$form [ 'type' ] = array ( '#type' => 'value' , '#value' => 'vocabulary' );
$form [ 'vid' ] = array ( '#type' => 'value' , '#value' => $vid );
2010-03-25 11:53:25 +00:00
$form [ 'machine_name' ] = array ( '#type' => 'value' , '#value' => $vocabulary -> machine_name );
2007-11-26 19:46:52 +00:00
$form [ 'name' ] = array ( '#type' => 'value' , '#value' => $vocabulary -> name );
$form [ 'reset_alphabetical' ] = array ( '#type' => 'value' , '#value' => TRUE );
return confirm_form ( $form ,
t ( 'Are you sure you want to reset the vocabulary %title to alphabetical order?' ,
array ( '%title' => $vocabulary -> name )),
2010-03-25 11:53:25 +00:00
'admin/structure/taxonomy/' . $vocabulary -> machine_name ,
2007-12-19 15:40:43 +00:00
t ( 'Resetting a vocabulary will discard all custom ordering and sort items alphabetically.' ),
t ( 'Reset to alphabetical' ),
2007-11-26 19:46:52 +00:00
t ( 'Cancel' ));
}
/**
* Submit handler to reset a vocabulary to alphabetical order after confirmation .
2007-11-28 10:29:21 +00:00
*
2008-01-08 10:35:43 +00:00
* @ see taxonomy_vocabulary_confirm_reset_alphabetical ()
2007-11-26 19:46:52 +00:00
*/
function taxonomy_vocabulary_confirm_reset_alphabetical_submit ( $form , & $form_state ) {
2009-04-15 14:12:55 +00:00
db_update ( 'taxonomy_term_data' )
-> fields ( array ( 'weight' => 0 ))
-> condition ( 'vid' , $form_state [ 'values' ][ 'vid' ])
-> execute ();
2007-11-26 19:46:52 +00:00
drupal_set_message ( t ( 'Reset vocabulary %name to alphabetical order.' , array ( '%name' => $form_state [ 'values' ][ 'name' ])));
watchdog ( 'taxonomy' , 'Reset vocabulary %name to alphabetical order.' , array ( '%name' => $form_state [ 'values' ][ 'name' ]), WATCHDOG_NOTICE );
2010-03-25 11:53:25 +00:00
$form_state [ 'redirect' ] = 'admin/structure/taxonomy/' . $form_state [ 'values' ][ 'machine_name' ];
2007-11-26 19:46:52 +00:00
}