You are here

function theme_fpa_user_admin_permissions in Fast Permissions Administration 8.2

Same name and namespace in other branches
  1. 7.2 fpa.theme.inc \theme_fpa_user_admin_permissions()

Theme function to pre-mark rows with FPA attributes.

Based on Drupal Core's permissions form theme function.

See also

theme_user_admin_permissions().

File

./fpa.theme.inc, line 27
Theme callbacks to pre-tag rows for FPA functionality.

Code

function theme_fpa_user_admin_permissions($variables) {
  $form = $variables['form'];
  $nameless_checkbox = [
    '#type' => 'html_tag',
    '#tag' => 'input',
    '#attributes' => [
      'type' => 'checkbox',
      'class' => [
        'rid-1',
        // Prevents Drupal core Drupal.behaviors.permissions.toggle from applying.
        'form-checkbox',
        'fpa-checkboxes-toggle',
      ],
    ],
  ];
  $nameless_checkbox_output = \Drupal::service('renderer')
    ->render($nameless_checkbox);
  $dummy_checkbox = [
    '#type' => 'html_tag',
    '#tag' => 'input',
    '#attributes' => [
      'type' => 'checkbox',
      'disabled' => 'disabled',
      'checked' => 'checked',
      'title' => t('This permission is inherited from the authenticated user role.'),
      'class' => [
        'dummy-checkbox',
      ],
    ],
  ];
  $dummy_checkbox_output = \Drupal::service('renderer')
    ->render($dummy_checkbox);
  $permission_col_template = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'fpa-permission-container',
      ],
    ],
    'description' => [
      '#markup' => '',
    ],
    'checkbox_cell' => [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'fpa-row-toggle-container',
        ],
      ],
      'checkbox_form_item' => [
        '#type' => 'container',
        '#attributes' => [
          'title' => t('Toggle visible checkboxes in this row.'),
          'class' => [
            'form-item',
            'form-type-checkbox',
          ],
        ],
        'label' => [
          '#type' => 'html_tag',
          '#tag' => 'label',
          '#attributes' => [
            'class' => [
              'element-invisible',
            ],
          ],
          '#value' => 'test',
        ],
        'checkbox' => [
          '#markup' => $nameless_checkbox_output,
        ],
      ],
    ],
  ];
  $checkboxes_children = element_children($form['checkboxes']);

  // Prepare role names processed by drupal_html_class() ahead of time.
  $roles_attr_values = [];
  foreach ($checkboxes_children as $rid) {
    $roles_attr_values[$rid] = drupal_html_class($form['role_names'][$rid]['#markup']);
  }
  $first_role_index = array_shift($checkboxes_children);

  // Lists for wrapper.
  $modules = [];
  $user_roles = [];

  // Index of current module row.
  $module = NULL;

  // Row counter.
  $i = 0;
  $rows = [];

  // Iterate over rows in form table.
  foreach (element_children($form['permission']) as $key) {

    // Row template.
    $row = [
      'data' => [],
      // Array of table cells.
      'title' => [],
      // HTML attribute on table row tag.
      FPA_ATTR_MODULE => [],
      // HTML attribute on table row tag.
      FPA_ATTR_PERMISSION => [],
      // HTML attribute on table row tag.
      FPA_ATTR_CHECKED => [],
      FPA_ATTR_NOT_CHECKED => [],
    ];

    // Determine if row is module or permission.
    if (is_numeric($key)) {

      // Module row.
      $row['class'][] = 'fpa-module-row';

      // Mark current row with escaped module name.
      $row[FPA_ATTR_MODULE] = [
        // System name
        0 => $form['permission'][$key]['#id'],
        // Readable name
        1 => strip_tags($form['permission'][$key]['#markup']),
      ];

      // Readable
      $row['data'][] = [
        'data' => \Drupal::service('renderer')
          ->render($form['permission'][$key]),
        'class' => [
          'module',
        ],
        'id' => 'module-' . $form['permission'][$key]['#id'],
        'colspan' => count($form['role_names']['#value']) + 1,
      ];
      $row['title'] = [
        $form['permission'][$key]['#id'],
      ];
      $row[FPA_ATTR_SYSTEM_NAME] = $row[FPA_ATTR_MODULE][0];
      $row[FPA_ATTR_MODULE] = array_unique(array_map('drupal_html_class', $row[FPA_ATTR_MODULE]));

      // Add modules to left-side modules list.
      $modules[$row[FPA_ATTR_MODULE][0]] = [
        'text' => strip_tags($form['permission'][$key]['#markup']),
        'title' => [
          $form['permission'][$key]['#id'],
        ],
        FPA_ATTR_MODULE => $row[FPA_ATTR_MODULE],
        FPA_ATTR_PERMISSION => [],
      ];

      // Save row number for current module.
      $module = $i;
    }
    else {

      // Permission row.
      $row['class'][] = 'fpa-permission-row';
      $permission_system_name = '';

      // Might be empty if no modules are displayed in Permissions Filter module.
      if (!empty($form['checkboxes'][$first_role_index])) {
        $permission_system_name = $form['checkboxes'][$first_role_index][$key]['#return_value'];
      }
      $label = $permission_col_template;
      $label['description']['#markup'] = \Drupal::service('renderer')
        ->render($form['permission'][$key]);

      // Permissions filter might cause no Roles to display.
      if (count(element_children($form['checkboxes'])) == 0) {
        unset($label['checkbox_cell']);
      }

      // Readable
      $row['data'][] = [
        'data' => \Drupal::service('renderer')
          ->render($label),
        'class' => [
          'permission',
        ],
      ];
      foreach (element_children($form['checkboxes']) as $rid) {
        $form['checkboxes'][$rid][$key]['#title'] = $form['role_names'][$rid]['#markup'] . ': ' . $form['permission'][$key]['#markup'];
        $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';

        // Filter permissions strips role id class from checkbox. Used by Drupal core functionality.
        $form['checkboxes'][$rid][$key]['#attributes']['class'][] = 'rid-' . $rid;

        // Set authenticated role behavior class on page load.
        if ($rid == 2 && $form['checkboxes'][$rid][$key]['#checked'] === TRUE) {
          $row['class'][] = 'fpa-authenticated-role-behavior';
        }

        // For all roles that inherit permissions from 'authenticated user' role, add in dummy checkbox for authenticated role behavior.
        if ($rid > 2) {
          $form['checkboxes'][$rid][$key]['#suffix'] = $dummy_checkbox_output;

          // '#suffix' doesn't have wrapping HTML like '#field_suffix'.
        }

        // Add rid's to row attribute for checked status filter.
        if ($form['checkboxes'][$rid][$key]['#checked'] === TRUE) {
          $row[FPA_ATTR_CHECKED][] = $rid;
        }
        else {
          $row[FPA_ATTR_NOT_CHECKED][] = $rid;
        }
        $row['data'][] = [
          'data' => \Drupal::service('renderer')
            ->render($form['checkboxes'][$rid][$key]),
          'class' => [
            'checkbox',
          ],
          'title' => [
            $form['role_names'][$rid]['#markup'],
          ],
          // For role filter
          FPA_ATTR_ROLE => [
            $rid,
          ],
        ];
      }
      if (!empty($rid)) {
        $row['title'] = [
          $form['checkboxes'][$rid][$key]['#return_value'],
        ];
        $row[FPA_ATTR_SYSTEM_NAME] = [
          $form['checkboxes'][$rid][$key]['#return_value'],
        ];
      }

      // Mark current row with escaped permission name.
      $row[FPA_ATTR_PERMISSION] = [
        // Permission system name.
        0 => $permission_system_name,
        // Readable description.
        1 => strip_tags($form['permission'][$key]['#markup']),
      ];

      // Mark current row with current module.
      $row[FPA_ATTR_MODULE] = $rows[$module][FPA_ATTR_MODULE];
      $row[FPA_ATTR_PERMISSION] = array_unique(array_map('drupal_html_class', $row[FPA_ATTR_PERMISSION]));

      // Add current permission to current module row.
      $rows[$module][FPA_ATTR_PERMISSION] = array_merge($rows[$module][FPA_ATTR_PERMISSION], $row[FPA_ATTR_PERMISSION]);
      $rows[$module][FPA_ATTR_CHECKED] = array_unique(array_merge($rows[$module][FPA_ATTR_CHECKED], $row[FPA_ATTR_CHECKED]));
      $rows[$module][FPA_ATTR_NOT_CHECKED] = array_unique(array_merge($rows[$module][FPA_ATTR_NOT_CHECKED], $row[FPA_ATTR_NOT_CHECKED]));
      $modules[$rows[$module][FPA_ATTR_MODULE][0]][FPA_ATTR_PERMISSION][] = $row[FPA_ATTR_PERMISSION];
    }
    $rows[$i++] = $row;
  }
  $reset_button = [
    '#type' => 'html_tag',
    '#tag' => 'input',
    '#attributes' => [
      'type' => 'reset',
      'class' => 'form-submit',
      'value' => t('Reset changes'),
    ],
  ];

  // If there is no submit button, don't add the reset button.
  if (count(element_children($form['actions'])) > 0) {

    // Have the reset button appear before the submit button.
    array_unshift($form['actions'], $reset_button);
  }
  foreach ($form['actions'] as $key) {
    if (!empty($form['actions'][$key])) {
      $actions_output .= \Drupal::service('renderer')
        ->render($form['actions'][$key]);
    }
  }
  $header = [];
  $header[] = [
    'data' => t('Permission') . $actions_output,
  ];
  foreach (element_children($form['role_names']) as $rid) {
    $header[] = [
      'data' => \Drupal::service('renderer')
        ->render($form['role_names'][$rid]) . $nameless_checkbox_output,
      'class' => [
        'checkbox',
      ],
      'title' => [
        $form['role_names'][$rid]['#markup'],
      ],
      FPA_ATTR_ROLE => [
        $rid,
      ],
    ];
    $user_roles[$rid] = $form['role_names'][$rid]['#markup'];
  }
  $table = [
    'header' => $header,
    'rows' => $rows,
  ];
  $output = _fpa_wrapper($table, $modules, $user_roles, $actions_output);
  foreach ($form as $key) {
    if (!empty($form[$key])) {
      $output .= \Drupal::service('renderer')
        ->render($form[$key]);
    }
  }
  return $output;
}