Chris@0: vocabularyStorage = $vocabulary_storage; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('entity.manager')->getStorage('taxonomy_vocabulary') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function form(array $form, FormStateInterface $form_state) { Chris@0: $vocabulary = $this->entity; Chris@0: if ($vocabulary->isNew()) { Chris@0: $form['#title'] = $this->t('Add vocabulary'); Chris@0: } Chris@0: else { Chris@0: $form['#title'] = $this->t('Edit vocabulary'); Chris@0: } Chris@0: Chris@0: $form['name'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Name'), Chris@0: '#default_value' => $vocabulary->label(), Chris@0: '#maxlength' => 255, Chris@0: '#required' => TRUE, Chris@0: ]; Chris@0: $form['vid'] = [ Chris@0: '#type' => 'machine_name', Chris@0: '#default_value' => $vocabulary->id(), Chris@0: '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, Chris@0: '#machine_name' => [ Chris@0: 'exists' => [$this, 'exists'], Chris@0: 'source' => ['name'], Chris@0: ], Chris@0: ]; Chris@0: $form['description'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Description'), Chris@0: '#default_value' => $vocabulary->getDescription(), Chris@0: ]; Chris@0: Chris@0: // $form['langcode'] is not wrapped in an Chris@0: // if ($this->moduleHandler->moduleExists('language')) check because the Chris@0: // language_select form element works also without the language module being Chris@0: // installed. https://www.drupal.org/node/1749954 documents the new element. Chris@0: $form['langcode'] = [ Chris@0: '#type' => 'language_select', Chris@0: '#title' => $this->t('Vocabulary language'), Chris@0: '#languages' => LanguageInterface::STATE_ALL, Chris@0: '#default_value' => $vocabulary->language()->getId(), Chris@0: ]; Chris@0: if ($this->moduleHandler->moduleExists('language')) { Chris@0: $form['default_terms_language'] = [ Chris@0: '#type' => 'details', Chris@17: '#title' => $this->t('Term language'), Chris@0: '#open' => TRUE, Chris@0: ]; Chris@0: $form['default_terms_language']['default_language'] = [ Chris@0: '#type' => 'language_configuration', Chris@0: '#entity_information' => [ Chris@0: 'entity_type' => 'taxonomy_term', Chris@0: 'bundle' => $vocabulary->id(), Chris@0: ], Chris@0: '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary->id()), Chris@0: ]; Chris@0: } Chris@0: // Set the hierarchy to "multiple parents" by default. This simplifies the Chris@0: // vocabulary form and standardizes the term form. Chris@0: $form['hierarchy'] = [ Chris@0: '#type' => 'value', Chris@0: '#value' => '0', Chris@0: ]; Chris@0: Chris@0: $form = parent::form($form, $form_state); Chris@0: return $this->protectBundleIdElement($form); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function save(array $form, FormStateInterface $form_state) { Chris@0: $vocabulary = $this->entity; Chris@0: Chris@0: // Prevent leading and trailing spaces in vocabulary names. Chris@0: $vocabulary->set('name', trim($vocabulary->label())); Chris@0: Chris@0: $status = $vocabulary->save(); Chris@18: $edit_link = $this->entity->toLink($this->t('Edit'), 'edit-form')->toString(); Chris@0: switch ($status) { Chris@0: case SAVED_NEW: Chris@17: $this->messenger()->addStatus($this->t('Created new vocabulary %name.', ['%name' => $vocabulary->label()])); Chris@0: $this->logger('taxonomy')->notice('Created new vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]); Chris@18: $form_state->setRedirectUrl($vocabulary->toUrl('overview-form')); Chris@0: break; Chris@0: Chris@0: case SAVED_UPDATED: Chris@17: $this->messenger()->addStatus($this->t('Updated vocabulary %name.', ['%name' => $vocabulary->label()])); Chris@0: $this->logger('taxonomy')->notice('Updated vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]); Chris@18: $form_state->setRedirectUrl($vocabulary->toUrl('collection')); Chris@0: break; Chris@0: } Chris@0: Chris@0: $form_state->setValue('vid', $vocabulary->id()); Chris@0: $form_state->set('vid', $vocabulary->id()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if the vocabulary already exists. Chris@0: * Chris@0: * @param string $vid Chris@0: * The vocabulary ID. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the vocabulary exists, FALSE otherwise. Chris@0: */ Chris@0: public function exists($vid) { Chris@0: $action = $this->vocabularyStorage->load($vid); Chris@0: return !empty($action); Chris@0: } Chris@0: Chris@0: }