You are here

VocabularyListBuilder.php in Termcase 8

Namespace

Drupal\termcase

File

src/VocabularyListBuilder.php
View source
<?php

namespace Drupal\termcase;

use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Defines a class to build a listing of taxonomy vocabulary entities.
 *
 * @see \Drupal\taxonomy\Entity\Vocabulary
 */
class VocabularyListBuilder extends DraggableListBuilder {

  /**
   * {@inheritdoc}
   */
  protected $entitiesKey = 'vocabularies';

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'taxonomy_overview_vocabularies';
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity) {
    $operations = parent::getDefaultOperations($entity);
    if (isset($operations['edit'])) {
      $operations['edit']['title'] = $this
        ->t('Edit vocabulary');
    }
    $operations['list'] = [
      'title' => $this
        ->t('List terms'),
      'weight' => 0,
      'url' => $entity
        ->toUrl('overview-form'),
    ];
    $operations['add'] = [
      'title' => $this
        ->t('Add terms'),
      'weight' => 10,
      'url' => Url::fromRoute('entity.taxonomy_term.add_form', [
        'taxonomy_vocabulary' => $entity
          ->id(),
      ]),
    ];
    unset($operations['delete']);
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this
      ->t('Vocabulary name');
    $header['termcase'] = $this
      ->t('Case Conversion');
    $header = $header + parent::buildHeader();
    return $header;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $row['label'] = $entity
      ->label();
    $vocab_machine_name = $entity
      ->get('vid');
    $config = \Drupal::config('termcase.settings');
    $mode_id = $config
      ->get('termcase_options_' . $vocab_machine_name);
    $termcase_mode = termcase_get_mode($mode_id);
    $row['termcase'] = [
      '#markup' => $termcase_mode,
    ];
    $row = $row + parent::buildRow($entity);
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $entities = $this
      ->load();

    // If there are not multiple vocabularies, disable dragging by unsetting the
    // weight key.
    if (count($entities) <= 1) {
      unset($this->weightKey);
    }
    $build = parent::render();
    $build['table']['#empty'] = $this
      ->t('No vocabularies available. <a href=":link">Add vocabulary</a>.', [
      ':link' => Url::fromRoute('entity.taxonomy_vocabulary.add_form')
        ->toString(),
    ]);
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $form['vocabularies']['#attributes'] = [
      'id' => 'taxonomy',
    ];
    $form['actions']['submit']['#value'] = $this
      ->t('Save');
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    \Drupal::messenger()
      ->addMessage($this
      ->t('The configuration options have been saved.'));
  }

}

Classes

Namesort descending Description
VocabularyListBuilder Defines a class to build a listing of taxonomy vocabulary entities.