0

I've got a few taxonomy vocabularies, but the confirmation message on save/update is generic - Created new term %term. or Updated term %term.. Those messages come from \Drupal\taxonomy\TermForm. Usage of that class is defined in \Drupal\taxonomy\Entity\Term class annotation.

I would like to have custom confirmation messages specific to each vocabulary.

What's the best way to accomplish that? Ideally I'd like to have separate form handler classes for each vocabulary.

2

Don't know if that's the best way, but it worked for me.

In your my_module.module:

/**
 * Implements hook_entity_type_alter().
 *
 * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
 *
 */
function my_module_entity_type_alter(array &$entity_types)
{
    $entity_types['taxonomy_term']->setFormClass('default', '\Drupal\my_module\Form\MyTermForm');
}

Create my_module/src/Form/MyTermForm.php and override the save() method of Drupal\taxonomy\TermForm.

/**
 * @file
 * Contains \Drupal\my_module\Form\MyTermForm.
 */

namespace Drupal\my_module\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\TermForm;

class MyTermForm extends TermForm
{

    /**
     * {@inheritdoc}
     */
    public function save(array $form, FormStateInterface $form_state) {
        $term = $this->entity;

        $result = $term->save();

        $link = $term->link($this->t('Edit'), 'edit-form');
        switch ($result) {
            case SAVED_NEW:
                drupal_set_message($this->t('Created new :type %term.', array(':type' => $this->getType($term), '%term' => $term->getName())));
                $this->logger('taxonomy')->notice('Created new :type %term.', array(':type' => $this->getType($term), '%term' => $term->getName(), 'link' => $link));
                break;
            case SAVED_UPDATED:
                drupal_set_message($this->t('Updated :type %term.', array(':type' => $this->getType($term), '%term' => $term->getName())));
                $this->logger('taxonomy')->notice('Updated :type %term.', array(':type' => $this->getType($term), '%term' => $term->getName(), 'link' => $link));
                break;
        }

        $current_parent_count = count($form_state->getValue('parent'));
        $previous_parent_count = count($form_state->get(['taxonomy', 'parent']));
        // Root doesn't count if it's the only parent.
        if ($current_parent_count == 1 && $form_state->hasValue(array('parent', 0))) {
            $current_parent_count = 0;
            $form_state->setValue('parent', array());
        }

        // 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.
        $vocabulary = $form_state->get(['taxonomy', 'vocabulary']);
        if ($current_parent_count < $previous_parent_count && $current_parent_count < 2) {
            taxonomy_check_vocabulary_hierarchy($vocabulary, $form_state->getValues());
        }
        // 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 && $vocabulary->getHierarchy() != TAXONOMY_HIERARCHY_MULTIPLE) {
            $vocabulary->setHierarchy($current_parent_count == 1 ? TAXONOMY_HIERARCHY_SINGLE : TAXONOMY_HIERARCHY_MULTIPLE);
            $vocabulary->save();
        }

        $form_state->setValue('tid', $term->id());
        $form_state->set('tid', $term->id());
    }

    /**
     * @param \Drupal\taxonomy\TermInterface $term
     */
    private function getType($term)
    {
        $vocabularyId = $term->getVocabularyId();
        switch ($vocabularyId) {
            case 'pages':
                return 'page';
            default:
                return 'term';
        }
    }

}
| improve this answer | |

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.