function photos_access_form_node_form_alter in Album Photos 6.0.x
Same name and namespace in other branches
- 8.5 photos_access/photos_access.module \photos_access_form_node_form_alter()
- 8.4 photos_access/photos_access.module \photos_access_form_node_form_alter()
Implements hook_form_BASE_FORM_ID_alter() for form_node.
File
- photos_access/
photos_access.module, line 48 - Implementation of photos_access.module.
Code
function photos_access_form_node_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
// Get form node.
/** @var \Drupal\node\Entity\Node $node */
$node = $form_state
->getFormObject()
->getEntity();
if ($node && ($node_type = $node
->getType())) {
if (\Drupal::config('photos.settings')
->get('photos_access_' . $node_type)) {
$nid = $node
->id();
$form['photos_privacy'] = [
'#type' => 'details',
'#title' => t('Privacy'),
'#open' => TRUE,
'#weight' => 1,
'#tree' => TRUE,
];
// Access record(s) id.
$form['photos_privacy']['access_id'] = [
'#type' => 'value',
'#value' => isset($node->photos_privacy['access_id']) ? $node->photos_privacy['access_id'] : 0,
];
$form['photos_privacy']['vid'] = [
'#type' => 'value',
'#value' => isset($node->photos_privacy['vid']) ? $node->photos_privacy['vid'] : 0,
];
$form['photos_privacy']['eid'] = [
'#type' => 'value',
'#value' => isset($node->photos_privacy['eid']) ? $node->photos_privacy['eid'] : 0,
];
$old = [];
if ($nid) {
// Check collaborators and designated users.
if (!isset($node->photos_privacy['access_id']) || !($photos_album_access_id = $node->photos_privacy['access_id'])) {
$db = \Drupal::database();
$photos_album_access_id = $db
->query("SELECT id FROM {photos_access_album} WHERE nid = :nid", [
':nid' => $nid,
])
->fetchField();
}
if ($photos_album_access_id) {
$old['updateuser'] = _photos_access_userlist($photos_album_access_id, TRUE);
$old['viewuser'] = _photos_access_userlist($photos_album_access_id, FALSE);
}
}
$default_viewid = isset($node->photos_privacy['viewid']) ? $node->photos_privacy['viewid'] : 0;
// @todo add option(s) for external file systems as needed.
$privacy_options = [
0 => t('Open'),
1 => t('Locked'),
2 => t('Designated users'),
3 => t('Password required'),
];
// Prep role options. Roles with edit any can already edit this album.
// @note if "Authenticated user" has permission other roles wont inherit
// it automatically. A work-around is to uncheck authenticated user and
// check all other desired roles, then check authenticated users and save.
// Also, looks like these will be removed.
// @see https://www.drupal.org/project/drupal/issues/2025089
$user_roles_with_edit_own = user_role_names(TRUE, 'edit own photo');
$user_roles_with_edit_any = user_role_names(TRUE, 'edit any photo');
$role_options = array_diff($user_roles_with_edit_own, $user_roles_with_edit_any);
if (!empty($role_options)) {
$privacy_options[4] = t('Role access');
}
$form['photos_privacy']['viewid'] = [
'#type' => 'radios',
'#title' => t('Privacy'),
'#default_value' => $default_viewid,
'#options' => $privacy_options,
'#prefix' => '<div id="photos_access_privacy">',
'#suffix' => '</div>',
'#ajax' => [
'callback' => 'photos_access_privacy_form_ajax',
'event' => 'change',
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
],
];
// Prep password field.
$classes = ' class="photos-access-password photos-access-hidden-field"';
if ($default_viewid == 3) {
$classes = ' class="photos-access-password"';
}
$form['photos_privacy']['pass'] = [
'#type' => 'password',
'#title' => t('Password'),
'#default_value' => isset($node->photos_privacy['pass']) ? $node->photos_privacy['pass'] : '',
'#prefix' => '<div id="photos-access-password"' . $classes . '>',
'#suffix' => '</div>',
];
// Prep designated users field.
$classes = ' class="photos-access-view-users photos-access-hidden-field"';
if ($default_viewid == 2) {
$classes = ' class="photos-access-view-users"';
}
$userhelp = t('Separated by commas. eg: username1,username2,username3.');
$form['photos_privacy']['viewuser'] = [
'#type' => 'entity_autocomplete',
'#title' => t('Designated users'),
'#description' => t('Add people who will have access to view this node.') . ' ' . (isset($old['viewuser']) ? t('@help The following users have access:', [
'@help' => $userhelp,
]) . ' ' : $userhelp),
'#target_type' => 'user',
'#tags' => TRUE,
'#default_value' => isset($node->photos_privacy['viewuser']) && !is_array($node->photos_privacy['viewuser']) ? $node->photos_privacy['viewuser'] : NULL,
'#process_default_value' => FALSE,
'#prefix' => '<div id="photos-access-viewuser"' . $classes . '>',
'#suffix' => '</div>',
];
if (!empty($old['viewuser'])) {
foreach ($old['viewuser'] as $u) {
$form['photos_privacy']['viewremove'][$u['target_id']] = [
'#type' => 'checkbox',
'#default_value' => isset($node->viewremove[$u['target_id']]) ? $node->viewremove[$u['target_id']] : '',
'#title' => t('Delete: @name', [
'@name' => render($u['username']),
]),
'#prefix' => '<div id="photos-access-remove"' . $classes . '>',
'#suffix' => '</div>',
];
}
}
$form['photos_privacy']['updateuser'] = [
'#type' => 'entity_autocomplete',
'#title' => t('Add collaborators'),
'#target_type' => 'user',
'#tags' => TRUE,
'#default_value' => isset($node->photos_privacy['updateuser']) && !is_array($node->photos_privacy['updateuser']) ? $node->photos_privacy['updateuser'] : NULL,
'#description' => t('Add people who will have the authority to edit this node.') . ' ' . (isset($old['updateuser']) ? t('@help collaboration users list:', [
'@help' => $userhelp,
]) . ' ' : $userhelp),
];
if (!empty($old['updateuser'])) {
// @todo add option to delete all collaborators.
foreach ($old['updateuser'] as $u) {
$form['photos_privacy']['updateremove'][$u['target_id']] = [
'#type' => 'checkbox',
'#default_value' => isset($node->updateremove[$u['target_id']]) ? $node->updateremove[$u['target_id']] : '',
'#title' => t('Delete: @name', [
'@name' => render($u['username']),
]),
'#prefix' => '<div id="photos_access_updateremove" class="photos-access-update-remove">',
'#suffix' => '</div>',
];
}
}
// @todo the roles option should probably only be accessible by admins or
// users with special permissions?
// Prep roles form option.
$classes = ' class="photos-access-roles photos-access-hidden-field"';
if ($default_viewid == 4) {
$classes = ' class="photos-access-roles"';
}
// Prep default options.
$default_roles = [];
if (isset($node->photos_privacy['roles'])) {
foreach ($node->photos_privacy['roles'] as $role) {
if ($role) {
$default_roles[$role] = $role;
}
}
}
if (!empty($role_options)) {
$description = t('The selected roles will have access to view,
edit and delete this gallery depending on global user permissions to
edit own photos and delete own photos.');
}
else {
$description = t('Roles with permission to "Edit own photos" are
required for this feature.');
}
$form['photos_privacy']['roles'] = [
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $role_options),
'#default_value' => $default_roles,
'#description' => $description,
'#prefix' => '<div id="photos-access-roles"' . $classes . '>',
'#suffix' => '</div>',
];
$form['#attached']['library'][] = 'photos_access/photos_access.node.form';
// Make sure $node->photos_privacy is available in node_insert and
// node_update.
$form['#entity_builders'][] = 'photos_access_node_builder';
// Validate password and users.
$form['#validate'][] = 'photos_access_node_validate';
}
if ($node_type == 'photos') {
// Move files if needed.
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'photos_access_move_files_form_submit';
}
}
}
}
}