3 namespace Drupal\workspaces\Form;
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Entity\EntityConstraintViolationListInterface;
8 use Drupal\Core\Entity\EntityRepositoryInterface;
9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Messenger\MessengerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Form controller for the workspace edit forms.
18 class WorkspaceForm extends ContentEntityForm implements WorkspaceFormInterface {
21 * The workspace entity.
23 * @var \Drupal\workspaces\WorkspaceInterface
28 * The messenger service.
30 * @var \Drupal\Core\Messenger\MessengerInterface
35 * Constructs a new WorkspaceForm.
37 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
38 * The entity repository service.
39 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
40 * The messenger service.
41 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
42 * The entity type bundle service.
43 * @param \Drupal\Component\Datetime\TimeInterface $time
46 public function __construct(EntityRepositoryInterface $entity_repository, MessengerInterface $messenger, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
47 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
48 $this->messenger = $messenger;
54 public static function create(ContainerInterface $container) {
56 $container->get('entity.repository'),
57 $container->get('messenger'),
58 $container->get('entity_type.bundle.info'),
59 $container->get('datetime.time')
66 public function form(array $form, FormStateInterface $form_state) {
67 $workspace = $this->entity;
69 if ($this->operation == 'edit') {
70 $form['#title'] = $this->t('Edit workspace %label', ['%label' => $workspace->label()]);
73 '#type' => 'textfield',
74 '#title' => $this->t('Label'),
76 '#default_value' => $workspace->label(),
81 '#type' => 'machine_name',
82 '#title' => $this->t('Workspace ID'),
84 '#default_value' => $workspace->id(),
85 '#disabled' => !$workspace->isNew(),
87 'exists' => '\Drupal\workspaces\Entity\Workspace::load',
89 '#element_validate' => [],
92 return parent::form($form, $form_state);
98 protected function getEditedFieldNames(FormStateInterface $form_state) {
102 ], parent::getEditedFieldNames($form_state));
108 protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
109 // Manually flag violations of fields not handled by the form display. This
110 // is necessary as entity form displays only flag violations for fields
111 // contained in the display.
116 foreach ($violations->getByFields($field_names) as $violation) {
117 list($field_name) = explode('.', $violation->getPropertyPath(), 2);
118 $form_state->setErrorByName($field_name, $violation->getMessage());
120 parent::flagViolations($violations, $form, $form_state);
126 public function save(array $form, FormStateInterface $form_state) {
127 $workspace = $this->entity;
128 $workspace->setNewRevision(TRUE);
129 $status = $workspace->save();
131 $info = ['%info' => $workspace->label()];
132 $context = ['@type' => $workspace->bundle(), '%info' => $workspace->label()];
133 $logger = $this->logger('workspaces');
135 if ($status == SAVED_UPDATED) {
136 $logger->notice('@type: updated %info.', $context);
137 $this->messenger->addMessage($this->t('Workspace %info has been updated.', $info));
140 $logger->notice('@type: added %info.', $context);
141 $this->messenger->addMessage($this->t('Workspace %info has been created.', $info));
144 if ($workspace->id()) {
145 $form_state->setValue('id', $workspace->id());
146 $form_state->set('id', $workspace->id());
148 $collection_url = $workspace->toUrl('collection');
149 $redirect = $collection_url->access() ? $collection_url : Url::fromRoute('<front>');
150 $form_state->setRedirectUrl($redirect);
153 $this->messenger->addError($this->t('The workspace could not be saved.'));
154 $form_state->setRebuild();