PathFileEntityForm.php in Path File 8
File
src/Form/PathFileEntityForm.php
View source
<?php
namespace Drupal\path_file\Form;
use Drupal\path_file\Entity\PathFileEntityInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class PathFileEntityForm extends ContentEntityForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['#entity_builders']['update_status'] = [
$this,
'updateStatus',
];
return $form;
}
public function updateStatus($entity_type_id, PathFileEntityInterface $path_file, array $form, FormStateInterface $form_state) {
$element = $form_state
->getTriggeringElement();
if (isset($element['#published_status'])) {
$path_file
->setPublished($element['#published_status']);
}
}
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$path_file = $this->entity;
if (\Drupal::currentUser()
->hasPermission('Administer Path file entity entities')) {
$element['publish'] = $element['submit'];
$element['publish']['#published_status'] = TRUE;
$element['publish']['#dropbutton'] = 'save';
if ($path_file
->isNew()) {
$element['publish']['#value'] = t('Save and publish');
}
else {
$element['publish']['#value'] = $path_file
->isPublished() ? t('Save and keep published') : t('Save and publish');
}
$element['publish']['#weight'] = 0;
$element['unpublish'] = $element['submit'];
$element['unpublish']['#published_status'] = FALSE;
$element['unpublish']['#dropbutton'] = 'save';
if ($path_file
->isNew()) {
$element['unpublish']['#value'] = t('Save as unpublished');
}
else {
$element['unpublish']['#value'] = !$path_file
->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
}
$element['unpublish']['#weight'] = 10;
if ($path_file
->isPublished()) {
unset($element['unpublish']['#button_type']);
}
else {
unset($element['publish']['#button_type']);
$element['unpublish']['#weight'] = -10;
}
$element['submit']['#access'] = FALSE;
}
$element['delete']['#access'] = $path_file
->access('delete');
$element['delete']['#weight'] = 100;
return $element;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label Path file entity.', [
'%label' => $entity
->label(),
]));
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label Path file entity.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.path_file_entity.collection');
}
}