0

I would like to change the design of the taxonomy add/edit term form, how do you create a template for the taxonomy "Add Term" / "Edit Term" just like creating a node form template? Not creating a custom module because I already have some custom fields for my term.

As of now this is the code on my template.tpl

'admin/structure/taxonomy/type_of_category/add' => array(
        'arguments' => array(
            'form' => NULL,
            ),
       'template' => 'templates/type-of-build-your-event-category-taxonomy-form-term', // set the path here if not in root theme directory
       'render element' => 'form',
    )

These are the filenames I've tried for my template file:

  • taxonomy-term--type_of_category-taxonomy-form-term.tpl.php
  • type_of_category-taxonomy-form-term.tpl.php
  • taxonomy-form-term.tpl.php
  • type_of_category.tpl.php

None of it works.

Does anyone have any suggestion on how to make this work?

Thank you in advance!

1

Use hook_theme() inside your theme's template.php file to set a custom template file (tpl.php) for the taxonomy add and edit forms. However, your theme needs to be set as the Administration Theme, even if Use the administration theme when editing or creating content is not checked.

The taxonomy module implements only one form template file for the taxonomy add and edit forms for all vocabularies. In other words, the taxonomy module does not provide, on its own, a way to have a different taxonomy form template file for a specific vocabulary. However, it is possible for you to add this capability by implementing hook_form_alter() within your theme's template.php file (explained below).

PSA: clear your site's cache after making changes to your templatate.php file.

This is what would be in the template.php file for the theme_name administration theme if you wanted to theme the taxonomy add and edit forms for all vocabularies:

/**
 * Implements hook_theme().
 */
function theme_name_theme($existing, $type, $theme, $path) {
  $items['taxonomy_form_term'] = array(
    'render element' => 'form',
    'template' => 'taxonomy-form-term',
    'path' => drupal_get_path('theme', 'theme_name') . '/templates',
  );
  return $items;
}
  • $items['taxonomy_form_term'] = array(...: The key taxonomy_form_term indicates the values inside the array are to theme the taxonomy add and edit forms. This key is defined by the taxonomy module.
  • 'render element' => 'form': $form will be the variable that holds the renerable form array. Note that the words "render path" are separated by a space, and not an underscore.
  • 'template' => 'taxonomy-form-term': The name of the template file without the extension. When the extension is added, the name of the file will be taxonomy-form-term.tpl.php.
  • 'path' => drupal_get_path('theme', 'theme_name') . '/templates': The template file will be in the templates sub-directories within the theme_name theme's path. You are not required to use this exact directory, and can omit path from the array if you place the template file directly inside the theme's default path.

When you implement a theme hook, you will also have a preprocess function that you can implement in your theme's template.php file for that theme hook:

function theme_name_preprocess_taxonomy_form_term(&$variables){}

Create a file named taxonomy-form-term.tpl.php in the templates sub-directory of your theme, e.g. sites/all/themes/theme_name/templates/taxonomy-form-term.tpl.php. Use the $form variable for rendering the form.

// Move the Relations fieldset to the top of the form.
print render($form['relations']);
print drupal_render_children($form);

You can use hook_form_alter() to define keys for hook_theme() to theme the forms for specific vocabularies, specific terms, and/or for just the add (or edit) form. You add the keys to the $form['#theme'] array. You can name them whatever you want, but the order that they appear in the array matters. Keys at the beginning of the array take precedence over others, so add them starting with the most specific keys first. taxonomy_form_term,and possibly other keys, will already be in $form['#theme'], so you will need to prepend your keys to the beginning of the array.

/**
 * Implements hook_form_alter().
 */
function theme_name_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'taxonomy_form_term') {
    $term = $form['#term'];
    $vocabulary = $term['vocabulary_machine_name'];
    $tid = $term['tid'];

    $theme_keys = array();
    if (!$tid) {
      // Key to theme the 'add' form for this vocabulary.
      $theme_keys[] = 'taxonomy_form_' . $vocabulary . '_term_add';
      // Key to theme the add form for any vocabulary.
      $theme_keys[] = 'taxonomy_form_term_add';
    }
    else {
      // Key to theme the 'edit' form for this term using its term id.
      $theme_keys[] = 'taxonomy_form_term_edit_' . $tid;
      // Key to theme the 'edit' form for this term for this vocabulary, using
      // the term's name (after replacing non-alphanumeric characters with "_").
      $term_name = preg_replace('/[^a-z0-9]+/', '_', strtolower($term['name']));
      $theme_keys[] = 'taxonomy_form_' . $vocabulary . '_term_edit_' . $term_name;
      // Key to theme the 'edit' form for this vocabulary.
      $theme_keys[] = 'taxonomy_form_' . $vocabulary . '_term_edit';
    }
    // Key to theme the add/edit form for this vocabulary.
    $theme_keys[] = 'taxonomy_form_' . $vocabulary . '_term';

    // Add the theme keys to the beginning of the #theme array so they have
    // precedence over any preexisting ones.
    $form['#theme'] = array_merge($theme_keys, $form['#theme']);
  }
}

Implement the new theme hooks in your hook_theme(). For example, if you wanted to theme the edit form for the my_vocabulary vocabulary:

$items['taxonomy_form_my_vocabulary_term_edit'] = array(
  'render element' => 'form',
  'template' => 'taxonomy-form-my-vocabulary-term-edit',
  'path' => drupal_get_path('theme', 'theme_name') . '/templates',
);

You will also have a new preprocess function that you can implement in your theme's template.php file:

function theme_name_preprocess_taxonomy_form_my_vocabulary_term_edit(&$variables){}

Create the taxonomy-form-my-vocabulary-term-edit.tpl.php file in the templates sub-directory of your theme, and use the $form variable to render the form.

0

You can try this from drupal document https://www.drupal.org/docs/7/theming/advanced/theming-forms-in-your-theme

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.