public function ApiProductAccessControlForm::buildForm in Apigee Edge 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ ApiProductAccessControlForm.php, line 81
Class
- ApiProductAccessControlForm
- Provides a form for configuring visible API products in Drupal.
Namespace
Drupal\apigee_edge\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$role_storage = $this->entityTypeManager
->getStorage('user_role');
$role_names = [];
$roles_with_bypass_perm = [];
$form['access'] = [
'#type' => 'details',
'#title' => $this
->t('Access by visibility'),
'#description' => $this
->t('Limit access to API products by "Access" settings on Apigee Edge.'),
'#open' => TRUE,
'#tree' => TRUE,
];
/** @var \Drupal\user\RoleInterface $role */
foreach ($role_storage
->loadMultiple() as $role_name => $role) {
// Retrieve role names for columns.
$role_names[$role_name] = $role
->label();
$roles_with_bypass_perm[$role_name] = in_array('bypass api product access control', $role
->getPermissions()) || $role
->isAdmin();
}
// Store $role_names for use when saving the data.
$form['access']['role_names'] = [
'#type' => 'value',
'#value' => $role_names,
];
// Store $rolesWithBypassPerm for use when saving the data.
$form['access']['roles_with_bypass'] = [
'#type' => 'value',
'#value' => $roles_with_bypass_perm,
];
$form['access']['visibility'] = [
'#type' => 'table',
'#header' => [
t('Visibility'),
],
'#id' => 'visibility',
'#attributes' => [
'class' => [
'visibility',
'js-visibility',
],
],
'#sticky' => TRUE,
];
foreach ($role_names as $name) {
$form['access']['visibility']['#header'][] = [
'data' => $name,
'class' => [
'checkbox',
],
];
}
$visibilities = [
'public' => $this
->t('Public'),
'private' => $this
->t('Private'),
'internal' => $this
->t('Internal'),
];
// Pass this information to the form submit handler.
$form['access']['visibility']['options'] = [
'#type' => 'value',
'#value' => $visibilities,
];
foreach ($visibilities as $visibility => $label) {
$selected_roles = $this
->config('apigee_edge.api_product_settings')
->get('access')[$visibility] ?? [];
$form['access']['visibility'][$visibility]['name'] = [
'#markup' => $label,
];
foreach ($role_names as $rid => $name) {
$form['access']['visibility'][$visibility][$rid] = [
'#title' => $label,
'#title_display' => 'invisible',
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
'#type' => 'checkbox',
'#default_value' => in_array($rid, $selected_roles) ? 1 : 0,
'#attributes' => [
'class' => [
'rid-' . $rid,
'js-rid-' . $rid,
],
],
'#parents' => [
'access',
'visibility',
$rid,
$visibility,
],
];
// Show a column of disabled but checked checkboxes.
if ($roles_with_bypass_perm[$rid]) {
$form['access']['visibility'][$visibility][$rid]['#disabled'] = TRUE;
$form['access']['visibility'][$visibility][$rid]['#default_value'] = TRUE;
$form['access']['visibility'][$visibility][$rid]['#attributes']['title'] = $this
->t('This checkbox is disabled because this role has "Bypass API product access control" permission.');
}
}
}
$form['#attached']['library'][] = 'apigee_edge/apiproduct_access_admin';
return parent::buildForm($form, $form_state);
}