Provide GinContentFormBase class for entity forms

This commit is contained in:
Paul Weidner 2023-09-07 16:20:08 -07:00 committed by Michael Stenta
parent 6dc23e7054
commit 55e999af27
2 changed files with 53 additions and 0 deletions

View File

@ -8,6 +8,7 @@
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\farm_ui_theme\Form\GinContentFormBase;
/**
* Implements hook_theme().
@ -131,6 +132,25 @@ function farm_ui_theme_gin_content_form_routes() {
return $routes;
}
/**
* Implements hook_entity_type_build().
*/
function farm_ui_theme_entity_type_build(array &$entity_types) {
// Override the default add and edit form class.
$target_entity_types = [
'asset' => GinContentFormBase::class,
'log' => GinContentFormBase::class,
'plan' => GinContentFormBase::class,
];
foreach ($target_entity_types as $entity_type => $form_class) {
if (isset($entity_types[$entity_type])) {
$entity_types[$entity_type]->setFormClass('add', $form_class);
$entity_types[$entity_type]->setFormClass('edit', $form_class);
}
}
}
/**
* Implements hook_element_info_alter().
*/

View File

@ -0,0 +1,33 @@
<?php
namespace Drupal\farm_ui_theme\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\RenderCallbackInterface;
/**
* Entity form for gin content form styling.
*/
class GinContentFormBase extends ContentEntityForm implements RenderCallbackInterface {
/**
* {@inheritdoc}
*/
protected function getNewRevisionDefault() {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
$entity_type_label = $this->entity->getEntityType()->getSingularLabel();
$entity_url = $this->entity->toUrl()->setAbsolute()->toString();
$this->messenger()->addMessage($this->t('Saved %entity_type_label: <a href=":url">%label</a>', ['%entity_type_label' => $entity_type_label, ':url' => $entity_url, '%label' => $this->entity->label()]));
$form_state->setRedirectUrl($this->entity->toUrl());
return $status;
}
}