You are here

protected function FieldPermissionsController::buildRow in Field Permissions 8

Same name and namespace in other branches
  1. 8.2 src/Controller/FieldPermissionsController.php \Drupal\field_permissions\Controller\FieldPermissionsController::buildRow()

Build a single row.

Parameters

\Drupal\field\FieldStorageConfigInterface $field_storage: Field to populate row.

Return value

array Build row.

1 call to FieldPermissionsController::buildRow()
FieldPermissionsController::buildRows in src/Controller/FieldPermissionsController.php
Build table rows.

File

src/Controller/FieldPermissionsController.php, line 134

Class

FieldPermissionsController
Define FieldPermissionsController.

Namespace

Drupal\field_permissions\Controller

Code

protected function buildRow(FieldStorageConfigInterface $field_storage) {
  $row = [];
  if ($field_storage
    ->isLocked()) {
    $row[0]['class'] = [
      'menu-disabled',
    ];
    $row[0]['data'] = $this
      ->t('@field_name (Locked)', [
      '@field_name' => $field_storage
        ->getName(),
    ]);
  }
  else {
    $row[0]['data'] = $field_storage
      ->getName();
  }
  $row[1]['data'] = $field_storage
    ->getType();
  $row[2]['data'] = $field_storage
    ->getTargetEntityTypeId();
  $row[3]['data'] = implode(", ", $field_storage
    ->getBundles());
  $default_type = $this->fieldPermissions
    ->fieldGetPermissionType($field_storage);
  $field_permissions = $this->fieldPermissions
    ->getPermissionsByRole();
  if ($default_type === FieldPermissionTypeInterface::ACCESS_PUBLIC) {
    $row[4]['data'] = $this
      ->t('Not set (Field inherits content permissions.)');
    $row[4]['colspan'] = 5;
  }
  else {
    $plugin = $this->permissionTypeManager
      ->createInstance($default_type, [], $field_storage);
    if ($plugin instanceof CustomPermissionsInterface) {

      // This is a field with custom permissions. Link the field to the
      // appropriate row of the permissions page, and theme it based on
      // whether all users have access.
      foreach (array_keys($plugin
        ->getPermissions()) as $index => $permission) {
        $all_access = in_array($permission, $field_permissions[RoleInterface::ANONYMOUS_ID]) && in_array($permission, $field_permissions[RoleInterface::AUTHENTICATED_ID]);
        $class = $all_access ? 'field-permissions-status-on' : 'field-permissions-status-off';
        $text = $all_access ? $this
          ->t('All users have this permission') : $this
          ->t('Not all users have this permission');
        $link = Link::createFromRoute($text, 'user.admin_permissions', [], [
          'fragment' => 'module-field_permissions',
        ])
          ->toRenderable();
        $link['#options']['attributes']['title'] = $text;
        $row[4 + $index]['data'] = $link;
        $row[4 + $index]['class'] = [
          $class,
        ];
      }
    }
    else {

      // Use the label and description.
      $row[4]['data'] = $this
        ->t('@label (@description)', [
        '@label' => $plugin
          ->getLabel(),
        '@description' => $plugin
          ->getDescription(),
      ]);
      $row[4]['colspan'] = 5;
    }
  }
  return $row;
}