account = $account;
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static(
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('current_user'),
$container->get('date.formatter')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/** @var CustomEntity $entity */
$entity = $this->entity;
// Advanced tab must be the first, because other fields rely on that.
if (!isset($form['advanced'])) {
$form['advanced'] = [
'#type' => 'vertical_tabs',
'#weight' => 99,
];
}
/* @var \Drupal\custom_entity\Entity\CustomEntity $entity */
$form = parent::buildForm($form, $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->account->hasPermission('administer product entities'),
];
$form['meta']['code'] = [
'#type' => 'item',
'#markup' => '' . $this->t('Product code') . ' ' . $entity->getCode(),
'#access' => !$entity->isNew(),
'#wrapper_attributes' => ['class' => ['entity-meta__title']],
];
$form['meta']['published'] = [
'#type' => 'item',
'#markup' => $entity->isPublished() ? $this->t('Published') : $this->t('Not published'),
'#access' => !$entity->isNew(),
'#wrapper_attributes' => ['class' => ['entity-meta__title']],
];
$form['meta']['changed'] = [
'#type' => 'item',
'#title' => $this->t('Last saved'),
'#markup' => ( !$entity->isNew() ? ( $this->dateFormatter->format($entity->getChangedTime(), 'short') . ( $entity->getRevisionLogMessage() ? ' - ' . $entity->getRevisionLogMessage() . '' : '' ) ) : $this->t('Not saved yet') ),
'#wrapper_attributes' => ['class' => ['entity-meta__last-saved']],
];
$form['meta']['author'] = [
'#type' => 'item',
'#title' => $this->t('Author'),
'#markup' => $entity->getOwner()->getAccountName(),
'#wrapper_attributes' => ['class' => ['entity-meta__author']],
];
$form['meta']['language'] = [
'#type' => 'item',
'#title' => $this->t('Language'),
'#markup' => $this->t($entity->language()->getName()),
'#wrapper_attributes' => ['class' => ['entity-meta__language']],
];
$form['options'] = [
'#type' => 'details',
'#group' => 'advanced',
'#weight' => -10,
'#title' => t('Authoring information'),
'#attributes' => ['class' => ['entity-meta__options']],
'#tree' => TRUE,
'#access' => $this->account->hasPermission('administer product entities'),
];
if (isset($form['status'])) {
$form['status']['#group'] = 'options';
}
if (isset($form['user_id'])) {
$form['user_id']['#group'] = 'options';
}
if (isset($form['langcode'])) {
$form['langcode']['#group'] = 'options';
}
$form['revision'] = [
'#type' => 'details',
'#group' => 'advanced',
'#weight' => -10,
'#title' => $this->t('Revision'),
'#attributes' => ['class' => ['entity-meta__revision']],
'#tree' => TRUE,
'#access' => $this->account->hasPermission('administer product entities'),
];
if (!$this->entity->isNew()) {
$form['revision']['new_revision'] = [
'#type' => 'checkbox',
'#title' => $this->t('Create new revision'),
'#default_value' => FALSE,
'#weight' => -10,
'#wrapper_attributes' => ['class' => ['entity-meta__new_revision']],
];
}
if (isset($form['revision_translation_affected'])) {
$form['revision_translation_affected']['#group'] = 'revision';
}
if (isset($form['revision_created'])) {
$form['revision_created']['#group'] = 'revision';
}
if (isset($form['revision_user'])) {
$form['revision_user']['#group'] = 'revision';
}
if (isset($form['revision_log_message'])) {
$form['revision_log_message']['#group'] = 'revision';
}
// dump($form); exit;
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var CustomEntity */
$entity = $this->entity;
// Save as a new revision if requested to do so.
if (!$form_state->isValueEmpty('new_revision') && $form_state->getValue('new_revision') != FALSE) {
$entity->setNewRevision();
// If a new revision is created, save the current user as revision author.
$entity->setRevisionCreationTime($this->time->getRequestTime());
$entity->setRevisionUserId($this->account->id());
}
else {
$entity->setNewRevision(FALSE);
}
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
$this->messenger()->addMessage($this->t('Created the %label Product.', [
'%label' => $entity->label() . ' (' . $entity->getCode() . ')',
]));
break;
default:
$this->messenger()->addMessage($this->t('Saved the %label Product.', [
'%label' => $entity->label() . ' (' . $entity->getCode() . ')',
]));
}
$form_state->setRedirect('entity.to_product.canonical', ['to_product' => $entity->id()]);
}
}