0

I rendered the form to create a taxonomy term via the Inline Entity Form module.

Is there a way to validate the values submitted in that form using hook_taxonomy_term_presave() or another hook?
I tried with hook_form_alter(), but it didn't work.

2 Answers 2

0

I assume you should be looking at hook_form_FORM_ID_alter instead of hook_taxonomy_term_presave for validating the term title before saving.

/**
* Implements hook_form_FORM_ID_alter().
**/
function mymodule_form_taxonomy_form_term_alter(&$form,  &$form_state, $form_id) {
  // Check for tags bundle.
  if($form['#bundle'] == 'tags') {
    // Apply custom validator.
    $form['#validate'][] = '_mymodule_custom_term_validator';
  }
}

My custom validator.

/**
 * My custom validator for tags.
 **/
function _mymodule_custom_term_validtor() {
  // Check for term name is test.
  if(form_state['values']['name'] == 'test') {
    form_set_error('name', 'Title cannot be "test"');
  }
}
1
  • I rendered taxonomy add term form via "Inline Entity Form" module and here form_alter fails when it comes to validation. Any other way if hook_entity_presave can't do this? Aug 31, 2017 at 8:01
0

hook_taxonomy_term_presave() is not thought for validating what submitted in a form. In fact, it doesn't have any way to stop Drupal from saving the taxonomy term, nor does it allow users to correct the wrongly submitted value. That is what a validation handler does.

In this case, since the form to create a taxonomy term is added with the Inline Entity Form module, the hook to implement is hook_inline_entity_form_entity_form_alter().

function mymodule_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  if ($entity_form['#entity_type'] == 'taxonomy_term') {
    // Add the form validation handler.
  }
}

The form elements are added by TaxonomyTermInlineEntityFormController::entityForm() which uses the following code.

$entity_form['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#default_value' => $term->name,
  '#maxlength' => 255,
  '#required' => TRUE,
  '#weight' => !empty($extra_fields['name']) ? $extra_fields['name']['weight'] : -5,
);
$entity_form['description'] = array(
  '#type' => 'text_format',
  '#title' => t('Description'),
  '#default_value' => $term->description,
  '#format' => $term->format,
  '#weight' => !empty($extra_fields['description']) ? $extra_fields['description']['weight'] : -4,
);

$langcode = entity_language('taxonomy_term', $term);
field_attach_form('taxonomy_term', $term, $entity_form, $form_state, $langcode);
2
  • I rendered taxonomy add term form via "Inline Entity Form" module and here form_alter fails when it comes to validation. Any other way if hook_entity_presave can't do this? Aug 31, 2017 at 8:01
  • A validation handler is the only way.
    – apaderno
    Aug 31, 2017 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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