Skip to content

Commit

Permalink
Merge pull request #806 from UN-OCHA/berliner/base-object-warning-cle…
Browse files Browse the repository at this point in the history
…anup

Don't show base object warning after save
  • Loading branch information
berliner authored Nov 16, 2023
2 parents 2e2c6bf + bab0296 commit 5bb8020
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
30 changes: 0 additions & 30 deletions html/modules/custom/ghi_base_objects/ghi_base_objects.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\ghi_base_objects\Form\BaseObjectForm;
use Drupal\ghi_base_objects\Helpers\BaseObjectHelper;
use Drupal\hpc_api\Query\EndpointQuery;
use Drupal\hpc_common\Helpers\ArrayHelper;
use Drupal\hpc_common\Helpers\CommonHelper;
use Drupal\layout_builder\SectionStorageInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateSourceInterface;
Expand Down Expand Up @@ -104,34 +102,6 @@ function ghi_base_objects_theme_suggestions_base_object(array $variables) {
return $suggestions;
}

/**
* Implements hook_form_alter().
*/
function ghi_base_objects_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
if (CommonHelper::isAjaxRequest()) {
return;
}
if (!$form_state->getFormObject() instanceof BaseObjectForm) {
return;
}

// Disable all form elements except the listed ones.
$allow_editing = [
'form_id',
'form_build_id',
'form_token',
'field_content',
'status',
];
foreach (Element::children($form) as $element_key) {
if (in_array($element_key, $allow_editing)) {
continue;
}
$form[$element_key]['#disabled'] = 'disabled';
}
\Drupal::messenger()->addWarning(t('Most of the data in this form is imported automatically from the HPC API and cannot be changed here.'));
}

/**
* Implements hook_form_FORM_ID_alter().
*
Expand Down
22 changes: 22 additions & 0 deletions html/modules/custom/ghi_base_objects/src/Form/BaseObjectForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Render\Element;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -36,6 +38,23 @@ public static function create(ContainerInterface $container) {
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#entity'] = $this->entity;
$form = parent::buildForm($form, $form_state);

// Disable all form elements except the listed ones.
$allow_editing = [
'form_id',
'form_build_id',
'form_token',
'field_content',
'status',
];
foreach (Element::children($form) as $element_key) {
if (in_array($element_key, $allow_editing)) {
continue;
}
$form[$element_key]['#disabled'] = 'disabled';
}

$this->messenger()->addWarning($this->t('Most of the data in this form is imported automatically from the HPC API and cannot be changed here.'));
return $form;
}

Expand All @@ -46,6 +65,9 @@ public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = parent::save($form, $form_state);

// Delete warnings first.
$this->messenger()->deleteByType(Messenger::TYPE_WARNING);

switch ($status) {
case SAVED_NEW:
$this->messenger()->addMessage($this->t('Created the %label Base object.', [
Expand Down

0 comments on commit 5bb8020

Please sign in to comment.