Commit e0ea6c67 authored by catch's avatar catch

Issue #2803875 by amateescu, timmillwood, mcdruid, shashikant_chauhan,...

Issue #2803875 by amateescu, timmillwood, mcdruid, shashikant_chauhan, sidharthap, Manuel Garcia, Berdir: Node form meta information should not come from a theme
parent 687d4cff
......@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -22,6 +23,13 @@ class NodeForm extends ContentEntityForm {
*/
protected $tempStoreFactory;
/**
* The Current User object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Constructs a NodeForm object.
*
......@@ -33,10 +41,13 @@ class NodeForm extends ContentEntityForm {
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) {
parent::__construct($entity_manager, $entity_type_bundle_info, $time);
$this->tempStoreFactory = $temp_store_factory;
$this->currentUser = $current_user;
}
/**
......@@ -47,7 +58,8 @@ public static function create(ContainerInterface $container) {
$container->get('entity.manager'),
$container->get('user.private_tempstore'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time')
$container->get('datetime.time'),
$container->get('current_user')
);
}
......@@ -102,6 +114,34 @@ public function form(array $form, FormStateInterface $form_state) {
$form['advanced']['#attributes']['class'][] = 'entity-meta';
$form['meta'] = [
'#type' => 'details',
'#group' => 'advanced',
'#weight' => -10,
'#title' => $this->t('Status'),
'#attributes' => ['class' => ['entity-meta__header']],
'#tree' => TRUE,
'#access' => $this->currentUser->hasPermission('administer nodes'),
];
$form['meta']['published'] = [
'#type' => 'item',
'#markup' => $node->isPublished() ? $this->t('Published') : $this->t('Not published'),
'#access' => !$node->isNew(),
'#wrapper_attributes' => ['class' => ['entity-meta__title']],
];
$form['meta']['changed'] = [
'#type' => 'item',
'#title' => $this->t('Last saved'),
'#markup' => !$node->isNew() ? format_date($node->getChangedTime(), 'short') : $this->t('Not saved yet'),
'#wrapper_attributes' => ['class' => ['entity-meta__last-saved']],
];
$form['meta']['author'] = [
'#type' => 'item',
'#title' => $this->t('Author'),
'#markup' => $node->getOwner()->getUsername(),
'#wrapper_attributes' => ['class' => ['entity-meta__author']],
];
$form['footer'] = [
'#type' => 'container',
'#weight' => 99,
......
......@@ -199,6 +199,43 @@ public function testNodeEditAuthoredBy() {
$this->assertIdentical($this->webUser->id(), $node->getOwner()->id());
}
/**
* Tests the node meta information.
*/
public function testNodeMetaInformation() {
// Check that regular users (i.e. without the 'administer nodes' permission)
// can not see the meta information.
$this->drupalLogin($this->webUser);
$this->drupalGet('node/add/page');
$this->assertNoText('Not saved yet');
// Create node to edit.
$edit['title[0][value]'] = $this->randomMachineName(8);
$edit['body[0][value]'] = $this->randomMachineName(16);
$this->drupalPostForm(NULL, $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->drupalGet("node/" . $node->id() . "/edit");
$this->assertNoText('Published');
$this->assertNoText(format_date($node->getChangedTime(), 'short'));
// Check that users with the 'administer nodes' permission can see the meta
// information.
$this->drupalLogin($this->adminUser);
$this->drupalGet('node/add/page');
$this->assertText('Not saved yet');
// Create node to edit.
$edit['title[0][value]'] = $this->randomMachineName(8);
$edit['body[0][value]'] = $this->randomMachineName(16);
$this->drupalPostForm(NULL, $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->drupalGet("node/" . $node->id() . "/edit");
$this->assertText('Published');
$this->assertText(format_date($node->getChangedTime(), 'short'));
}
/**
* Checks that the "authored by" works correctly with various values.
*
......
......@@ -17,6 +17,8 @@
padding: 1em 1.5em;
}
.entity-meta__title {
font-size: 1.231em;
font-weight: bold;
text-shadow: 0 1px 0 #fff;
margin: 0.25em 0;
}
......
......@@ -147,42 +147,18 @@ function seven_preprocess_maintenance_page(&$variables) {
/**
* Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
*
* Changes vertical tabs to container and adds meta information.
* Changes vertical tabs to container.
*/
function seven_form_node_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
$form['#theme'] = ['node_edit_form'];
$form['#attached']['library'][] = 'seven/node-form';
$form['advanced']['#type'] = 'container';
$is_new = !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet');
$form['meta'] = [
'#attributes' => ['class' => ['entity-meta__header']],
'#type' => 'container',
'#group' => 'advanced',
'#weight' => -100,
'published' => [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $node->isPublished() ? t('Published') : t('Not published'),
'#access' => !$node->isNew(),
'#attributes' => [
'class' => ['entity-meta__title'],
],
],
'changed' => [
'#type' => 'item',
'#wrapper_attributes' => ['class' => ['entity-meta__last-saved', 'container-inline']],
'#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
],
'author' => [
'#type' => 'item',
'#wrapper_attributes' => ['class' => ['author', 'container-inline']],
'#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $node->getOwner()->getUsername(),
],
];
$form['meta']['#type'] = 'container';
$form['meta']['#access'] = TRUE;
$form['meta']['changed']['#wrapper_attributes']['class'][] = 'container-inline';
$form['meta']['author']['#wrapper_attributes']['class'][] = 'container-inline';
$form['revision_information']['#type'] = 'container';
$form['revision_information']['#group'] = 'meta';
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment