You are here

function node_authlink_form_node_type_form_alter in Node authorize link 8

Same name and namespace in other branches
  1. 7 node_authlink.module \node_authlink_form_node_type_form_alter()

Alter of node_type_form.

File

./node_authlink.module, line 22
Node Authlink hooks and alters.

Code

function node_authlink_form_node_type_form_alter(&$form, FormStateInterface &$form_state) {
  if (!\Drupal::currentUser()
    ->hasPermission('configure node_authlink module')) {
    return;
  }

  /** @var \Drupal\node\Entity\NodeType $node_type */
  $node_type = $form_state
    ->getFormObject();
  $type = $node_type
    ->getEntity()
    ->id();
  $config = \Drupal::config('node_authlink.settings');
  $config_enable = $config
    ->get('enable');
  $config_grants = $config
    ->get('grants');
  $config_expire = $config
    ->get('expire');
  $form['node_authlink'] = [
    '#type' => 'details',
    '#title' => t('Node authorize link'),
    '#group' => 'additional_settings',
  ];
  $form['node_authlink']['node_authlink_enable'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable'),
    '#default_value' => isset($config_enable[$type]) ? $config_enable[$type] : 0,
    '#description' => t('Disable of this feature will cost erase of authorization keys of all nodes in this node type.'),
  ];
  $form['node_authlink']['node_authlink_grants'] = [
    '#type' => 'checkboxes',
    '#title' => t('Grants to give'),
    '#default_value' => isset($config_grants[$type]) ? $config_grants[$type] : [],
    '#options' => [
      'view' => t('View'),
      'update' => t('Update'),
      'delete' => t('Delete'),
    ],
    '#description' => t('What operations will be temporarily given to authorised user for the node. This not affect users who is authorised yet.'),
  ];

  // Time periods: none, 1 day, 1 week, 4 weeks, 3 months, 6 months, 1 year
  $time_periods = [
    0,
    86400,
    604800,
    2419200,
    7776000,
    15552000,
    31536000,
  ];
  $period = node_authlink_build_options($time_periods);
  $period[0] = '<' . t('disabled') . '>';
  $form['node_authlink']['node_authlink_expire'] = [
    '#type' => 'select',
    '#title' => t('Regenerate authkeys after'),
    '#default_value' => isset($config_expire[$type]) ? $config_expire[$type] : '',
    '#options' => $period,
    '#description' => t('Keys older than selected time will be regenerated by cron run.'),
  ];
  $form['node_authlink']['node_authlink_batch'] = [
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Batch operations'),
    '#description' => t('Affects all nodes in this node type.'),
  ];
  $form['node_authlink']['node_authlink_batch']['generate'] = [
    '#type' => 'submit',
    '#value' => t('Generate authkeys'),
    '#submit' => [
      'node_authlink_batch_generate',
    ],
  ];
  $form['node_authlink']['node_authlink_batch']['delete'] = [
    '#type' => 'submit',
    '#value' => t('Delete all authkeys'),
    '#submit' => [
      'node_authlink_batch_delete',
    ],
  ];
  $form['actions']['submit']['#submit'][] = 'node_authlink_form_node_type_form_alter_submit';
}