VocabularyListController.php 4.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php

/**
 * @file
 * Contains \Drupal\taxonomy\VocabularyListController.
 */

namespace Drupal\taxonomy;

use Drupal\Core\Config\Entity\ConfigEntityListController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormInterface;

/**
 * Provides a listing of vocabularies.
 */
class VocabularyListController extends ConfigEntityListController implements FormInterface {

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

  /**
   * {@inheritdoc}
   */
  public function getOperations(EntityInterface $entity) {
    $operations = parent::getOperations($entity);
    $uri = $entity->uri();

33 34 35 36
    if (isset($operations['edit'])) {
      $operations['edit']['title'] = t('edit vocabulary');
      $operations['edit']['href'] = $uri['path'] . '/edit';
    }
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    $operations['list'] = array(
      'title' => t('list terms'),
      'href' => $uri['path'],
      'options' => $uri['options'],
      'weight' => 0,
    );
    $operations['add'] = array(
      'title' => t('add terms'),
      'href' => $uri['path'] . '/add',
      'options' => $uri['options'],
      'weight' => 30,
    );
    unset($operations['delete']);

    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
59 60 61
    $header['label'] = t('Vocabulary name');
    $header['weight'] = t('Weight');
    return $header + parent::buildHeader();
62 63 64 65 66 67 68 69 70 71
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    // Override default values to markup elements.
    $row['#attributes']['class'][] = 'draggable';

    $row['label'] = array(
72
      '#markup' => $this->getLabel($entity),
73 74 75 76 77 78 79 80 81 82
    );
    $row['#weight'] = $entity->get('weight');
    // Add weight column.
    $row['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array('@title' => $entity->label())),
      '#title_display' => 'invisible',
      '#default_value' => $entity->get('weight'),
      '#attributes' => array('class' => array('weight')),
    );
83
    return $row + parent::buildRow($entity);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $entities = $this->load();
    if (count($entities) > 1) {
      // Creates a form for manipulating vocabulary weights if more then one
      // vocabulary exists.
      return drupal_get_form($this);
    }
    $build = array(
      '#theme' => 'table',
      '#header' => $this->buildHeader(),
      '#rows' => array(),
      '#empty' => t('No vocabularies available. <a href="@link">Add vocabulary</a>.', array('@link' => url('admin/structure/taxonomy/add'))),
    );
    unset($build['#header']['weight']);
    foreach ($entities as $entity) {
104 105
      $row['label'] = $this->getLabel($entity);
      $build['#rows'][$entity->id()] = $row + parent::buildRow($entity);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    $form['vocabularies'] = array(
      '#type' => 'table',
      '#header' => $this->buildHeader(),
      '#tabledrag' => array(
        array('order', 'sibling', 'weight'),
      ),
      '#attributes' => array(
        'id' => 'taxonomy',
      ),
    );

    foreach ($this->load() as $entity) {
      $form['vocabularies'][$entity->id()] = $this->buildRow($entity);
    }

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#button_type' => 'primary',
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, array &$form_state) {
    // No validation.
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    $vocabularies = $form_state['values']['vocabularies'];

    $entities = entity_load_multiple($this->entityType, array_keys($vocabularies));
    foreach ($vocabularies as $id => $value) {
      if (isset($entities[$id]) && $value['weight'] != $entities[$id]->get('weight')) {
        // Update changed weight.
        $entities[$id]->set('weight', $value['weight']);
        $entities[$id]->save();
      }
    }

    drupal_set_message(t('The configuration options have been saved.'));
  }

}