class RoleListController

Provides a listing of user roles.

Hierarchy

Expanded class hierarchy of RoleListController

File

drupal/core/modules/user/lib/Drupal/user/RoleListController.php, line 17
Contains \Drupal\user\RoleListController.

Namespace

Drupal\user
View source
class RoleListController extends ConfigEntityListController implements FormInterface {

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

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $row = parent::buildHeader();
    $row['label'] = t('Name');
    unset($row['id']);
    $row['weight'] = t('Weight');
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  public function getOperations(EntityInterface $entity) {
    $operations = parent::getOperations($entity);
    $operations['permissions'] = array(
      'title' => t('Edit permissions'),
      'href' => 'admin/people/permissions/' . $entity
        ->id(),
      'weight' => 20,
    );

    // Built-in roles could not be deleted or disabled.
    if (in_array($entity
      ->id(), array(
      DRUPAL_ANONYMOUS_RID,
      DRUPAL_AUTHENTICATED_RID,
    ))) {
      unset($operations['delete']);
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $row = parent::buildRow($entity);

    // Override default values to markup elements.
    $row['#attributes']['class'][] = 'draggable';
    unset($row['id']);
    $row['label'] = array(
      '#markup' => check_plain($row['label']),
    );
    $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',
        ),
      ),
    );
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    return drupal_get_form($this);
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    $form['entities'] = array(
      '#type' => 'table',
      '#header' => $this
        ->buildHeader(),
      '#empty' => t('There is no @label yet.', array(
        '@label' => $this->entityInfo['label'],
      )),
      '#tabledrag' => array(
        array(
          'order',
          'sibling',
          'weight',
        ),
      ),
    );
    foreach ($this
      ->load() as $entity) {
      $form['entities'][$entity
        ->id()] = $this
        ->buildRow($entity);
    }
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save order'),
      '#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) {
    $values = $form_state['values']['entities'];
    $entities = entity_load_multiple($this->entityType, array_keys($values));
    foreach ($values 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 role settings have been updated.'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigEntityListController::load public function Overrides Drupal\Core\Entity\EntityListController::load(). Overrides EntityListController::load 3
EntityListController::$entityInfo protected property The entity info array.
EntityListController::$entityType protected property The entity type name.
EntityListController::$moduleHandler protected property The module handler to invoke hooks on.
EntityListController::$storage protected property The entity storage controller class.
EntityListController::buildOperations public function Builds a renderable list of operation links for the entity. 1
EntityListController::createInstance public static function Instantiates a new instance of this entity controller. Overrides EntityControllerInterface::createInstance 1
EntityListController::getStorageController public function Implements \Drupal\Core\Entity\EntityListControllerInterface::getStorageController(). Overrides EntityListControllerInterface::getStorageController
EntityListController::__construct public function Constructs a new EntityListController object. 1
RoleListController::buildForm public function Form constructor. Overrides FormInterface::buildForm
RoleListController::buildHeader public function Builds the header row for the entity listing. Overrides EntityListController::buildHeader
RoleListController::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListController::buildRow
RoleListController::getFormID public function Returns a unique string identifying the form. Overrides FormInterface::getFormID
RoleListController::getOperations public function Overrides \Drupal\Core\Entity\EntityListController::getOperations(); Overrides ConfigEntityListController::getOperations
RoleListController::render public function Implements \Drupal\Core\Entity\EntityListControllerInterface::render(). Overrides EntityListController::render
RoleListController::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RoleListController::validateForm public function Form validation handler. Overrides FormInterface::validateForm