You are here

function private_content_form_node_form_alter in Private 8

Same name and namespace in other branches
  1. 8.2 private_content.module \private_content_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.

This module adds a simple checkbox to the node form labeled private. If the checkbox is labelled, only the node author and users with 'access private content' privileges may see it.

File

./private_content.module, line 146
A tremendously simple access control module -- it allows users to mark individual nodes as private; users with 'access private content' perms can read these nodes, while others cannot.

Code

function private_content_form_node_form_alter(&$form, &$form_state) {

  /** @var \Drupal\Node\NodeInterface $node */
  $node = $form_state
    ->getFormObject()
    ->getEntity();

  /** @var \Drupal\node\NodeTypeInterface $node_type */
  $node_type = $node->type->entity;
  $default = $node_type
    ->getThirdPartySetting('private_content', 'private', PRIVATE_ALLOWED);
  if ($default != PRIVATE_DISABLED || !empty($node->private->value)) {
    if ($node
      ->isNew()) {
      $privacy = $default > PRIVATE_ALLOWED;
    }
    else {
      $privacy = isset($node->private) ? $node->private->value : 0;
    }
    if (\Drupal::currentUser()
      ->hasPermission('mark content as private') && $default != PRIVATE_ALWAYS) {
      if (\Drupal::currentUser()
        ->hasPermission('administer nodes')) {
        $form['options']['private'] = array(
          '#type' => 'checkbox',
          '#title' => t('Make this post private'),
          '#attributes' => array(
            'title' => t('When checked, only users with proper access permissions will be able to see this post.'),
          ),
          '#default_value' => $privacy,
        );
      }
      else {
        $form['private'] = array(
          '#type' => 'checkbox',
          '#title' => t('Make this post private'),
          '#attributes' => array(
            'title' => t('When checked, only users with proper access permissions will be able to see this post.'),
          ),
          '#default_value' => $privacy,
          '#weight' => 99,
        );
      }
    }
    else {
      $form['private'] = array(
        '#type' => 'value',
        '#value' => $privacy,
      );
    }
  }
}