You are here

publishcontent.module in Publish Content 8

File

publishcontent.module
View source
<?php

/**
 * @file
 * Contains publishcontent.module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\publishcontent\Access\PublishContentAccess;

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function publishcontent_form_node_form_alter(&$form, FormStateInterface $form_state) {
  $config = \Drupal::config('publishcontent.settings');
  $form['status']['widget']['value']['#title'] = $config
    ->get('publish_text_value');
  if (!array_key_exists('#default_value', $form['status']['widget']['value'])) {
    $form['status']['widget']['value']['#default_value'] = 0;
  }
  $form['status']['widget']['value']['#default_value'] ? $form['meta']['published']['#markup'] = $config
    ->get('publish_text_value') : ($form['meta']['published']['#markup'] = $config
    ->get('unpublish_text_value'));
  if (empty($config) || empty($config
    ->get('ui_checkbox'))) {
    $form['status']['#disabled'] = TRUE;
    return;
  }
  if (!empty($form['status']) && empty($form['status']['#access'])) {
    $user = Drupal::currentUser();
    $node = $form_state
      ->getFormObject()
      ->getEntity();
    $publishContentAccess = new PublishContentAccess();
    $form['status']['#access'] = TRUE;
    if (!$publishContentAccess
      ->access($user, $node)
      ->isAllowed()) {
      $form['status']['#disabled'] = TRUE;
      $form['status']['widget']['value']['#description'] = t('You can not change the @published status.', [
        '@published' => $config
          ->get('publish_text_value'),
      ]);
    }
  }
}