"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/taxonomy/src/VocabularyForm.php" (26 Nov 2020, 4972 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "VocabularyForm.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\taxonomy;
    4 
    5 use Drupal\Core\Entity\BundleEntityFormBase;
    6 use Drupal\Core\Entity\EntityTypeInterface;
    7 use Drupal\Core\Form\FormStateInterface;
    8 use Drupal\Core\Language\LanguageInterface;
    9 use Drupal\language\Entity\ContentLanguageSettings;
   10 use Symfony\Component\DependencyInjection\ContainerInterface;
   11 
   12 /**
   13  * Base form for vocabulary edit forms.
   14  *
   15  * @internal
   16  */
   17 class VocabularyForm extends BundleEntityFormBase {
   18 
   19   /**
   20    * The vocabulary storage.
   21    *
   22    * @var \Drupal\taxonomy\VocabularyStorageInterface
   23    */
   24   protected $vocabularyStorage;
   25 
   26   /**
   27    * Constructs a new vocabulary form.
   28    *
   29    * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
   30    *   The vocabulary storage.
   31    */
   32   public function __construct(VocabularyStorageInterface $vocabulary_storage) {
   33     $this->vocabularyStorage = $vocabulary_storage;
   34   }
   35 
   36   /**
   37    * {@inheritdoc}
   38    */
   39   public static function create(ContainerInterface $container) {
   40     return new static(
   41       $container->get('entity_type.manager')->getStorage('taxonomy_vocabulary')
   42     );
   43   }
   44 
   45   /**
   46    * {@inheritdoc}
   47    */
   48   public function form(array $form, FormStateInterface $form_state) {
   49     $vocabulary = $this->entity;
   50     if ($vocabulary->isNew()) {
   51       $form['#title'] = $this->t('Add vocabulary');
   52     }
   53     else {
   54       $form['#title'] = $this->t('Edit vocabulary');
   55     }
   56 
   57     $form['name'] = [
   58       '#type' => 'textfield',
   59       '#title' => $this->t('Name'),
   60       '#default_value' => $vocabulary->label(),
   61       '#maxlength' => 255,
   62       '#required' => TRUE,
   63     ];
   64     $form['vid'] = [
   65       '#type' => 'machine_name',
   66       '#default_value' => $vocabulary->id(),
   67       '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
   68       '#machine_name' => [
   69         'exists' => [$this, 'exists'],
   70         'source' => ['name'],
   71       ],
   72     ];
   73     $form['description'] = [
   74       '#type' => 'textfield',
   75       '#title' => $this->t('Description'),
   76       '#default_value' => $vocabulary->getDescription(),
   77     ];
   78 
   79     // $form['langcode'] is not wrapped in an
   80     // if ($this->moduleHandler->moduleExists('language')) check because the
   81     // language_select form element works also without the language module being
   82     // installed. https://www.drupal.org/node/1749954 documents the new element.
   83     $form['langcode'] = [
   84       '#type' => 'language_select',
   85       '#title' => $this->t('Vocabulary language'),
   86       '#languages' => LanguageInterface::STATE_ALL,
   87       '#default_value' => $vocabulary->language()->getId(),
   88     ];
   89     if ($this->moduleHandler->moduleExists('language')) {
   90       $form['default_terms_language'] = [
   91         '#type' => 'details',
   92         '#title' => $this->t('Term language'),
   93         '#open' => TRUE,
   94       ];
   95       $form['default_terms_language']['default_language'] = [
   96         '#type' => 'language_configuration',
   97         '#entity_information' => [
   98           'entity_type' => 'taxonomy_term',
   99           'bundle' => $vocabulary->id(),
  100         ],
  101         '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary->id()),
  102       ];
  103     }
  104     // Set the hierarchy to "multiple parents" by default. This simplifies the
  105     // vocabulary form and standardizes the term form.
  106     $form['hierarchy'] = [
  107       '#type' => 'value',
  108       '#value' => '0',
  109     ];
  110 
  111     $form = parent::form($form, $form_state);
  112     return $this->protectBundleIdElement($form);
  113   }
  114 
  115   /**
  116    * {@inheritdoc}
  117    */
  118   public function save(array $form, FormStateInterface $form_state) {
  119     $vocabulary = $this->entity;
  120 
  121     // Prevent leading and trailing spaces in vocabulary names.
  122     $vocabulary->set('name', trim($vocabulary->label()));
  123 
  124     $status = $vocabulary->save();
  125     $edit_link = $this->entity->toLink($this->t('Edit'), 'edit-form')->toString();
  126     switch ($status) {
  127       case SAVED_NEW:
  128         $this->messenger()->addStatus($this->t('Created new vocabulary %name.', ['%name' => $vocabulary->label()]));
  129         $this->logger('taxonomy')->notice('Created new vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]);
  130         $form_state->setRedirectUrl($vocabulary->toUrl('overview-form'));
  131         break;
  132 
  133       case SAVED_UPDATED:
  134         $this->messenger()->addStatus($this->t('Updated vocabulary %name.', ['%name' => $vocabulary->label()]));
  135         $this->logger('taxonomy')->notice('Updated vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]);
  136         $form_state->setRedirectUrl($vocabulary->toUrl('collection'));
  137         break;
  138     }
  139 
  140     $form_state->setValue('vid', $vocabulary->id());
  141     $form_state->set('vid', $vocabulary->id());
  142   }
  143 
  144   /**
  145    * Determines if the vocabulary already exists.
  146    *
  147    * @param string $vid
  148    *   The vocabulary ID.
  149    *
  150    * @return bool
  151    *   TRUE if the vocabulary exists, FALSE otherwise.
  152    */
  153   public function exists($vid) {
  154     $action = $this->vocabularyStorage->load($vid);
  155     return !empty($action);
  156   }
  157 
  158 }