Skip to content

Commit

Permalink
only override needed form fields for content release form
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfinnarn committed Jan 19, 2024
1 parent 6506bb1 commit 98a3e8b
Showing 1 changed file with 5 additions and 187 deletions.
192 changes: 5 additions & 187 deletions docroot/modules/custom/va_gov_content_release/src/Form/NextGitForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface;
use Drupal\va_gov_content_release\Frontend\Frontend;
use Drupal\va_gov_content_release\Frontend\FrontendInterface;
use Drupal\va_gov_content_release\FrontendVersion\FrontendVersionInterface;
use Drupal\va_gov_content_release\Reporter\ReporterInterface;
use Drupal\va_gov_content_release\Request\RequestInterface;
Expand All @@ -15,7 +13,7 @@
/**
* A version of the form allowing selection of a Git branch, tag, or commit.
*/
class NextGitForm extends BaseForm {
class NextGitForm extends GitForm {

/**
* The frontend version service.
Expand Down Expand Up @@ -52,7 +50,7 @@ public function __construct(
FrontendVersionInterface $frontendVersion,
BlockManagerInterface $blockManager
) {
parent::__construct($request, $reporter, $releaseStateManager);
parent::__construct($request, $reporter, $releaseStateManager, $frontendVersion, $blockManager);
$this->frontendVersion = $frontendVersion;
$this->blockManager = $blockManager;
}
Expand Down Expand Up @@ -81,33 +79,12 @@ public static function create(ContainerInterface $container) {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);

$form['build_request']['actions']['#type'] = 'actions';
$form['build_request']['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Release content'),
'#button_type' => 'primary',
];

$form['description'] = [
'#prefix' => '<p>',
'#markup' => $this->t('Release content to update the front end of this environment with the latest published content changes.'),
'#suffix' => '</p>',
'#weight' => -10,
];

$form['build_request']['title'] = [
'#type' => 'item',
'#prefix' => '<h2>',
'#markup' => $this->t('Request a content release'),
'#suffix' => '</h2>',
];

$form['build_request']['content_build_selection'] = [
'#title' => $this->t('Which version of content-build would you like to use?'),
'#title' => $this->t('Which version of next-build would you like to use?'),
'#type' => 'radios',
'#options' => [
'default' => $this->t('Use default - the content-build version from the time this demo environment was created.'),
'choose' => $this->t('Select a different content-build branch/pull request - for example, to see your content in a newer frontend design.'),
'default' => $this->t('Use default - the next-build version from the time this demo environment was created.'),
'choose' => $this->t('Select a different next-build branch/pull request - for example, to see your content in a newer frontend design.'),
],
'#default_value' => 'default',
];
Expand All @@ -129,166 +106,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
],
];

$form['build_request']['vets_website_selection'] = [
'#title' => $this->t('Which version of vets-website would you like to use?'),
'#type' => 'radios',
'#options' => [
'default' => $this->t('Use default - the vets-website version from the time this demo environment was created.'),
'choose' => $this->t('Select a different vets-website branch/pull request - for example, to see your content in a newer frontend design.'),
],
'#default_value' => 'default',
];

$form['build_request']['vets_website_git_ref'] = [
'#type' => 'textfield',
'#title' => $this->t('Select branch/pull request'),
'#description' => $this->t('Start typing to select a branch for the vets-website version you want to use.'),
'#autocomplete_route_name' => 'va_gov_content_release.frontend_version_autocomplete',
'#autocomplete_route_parameters' => [
'frontend' => 'vets_website',
'count' => 10,
],
'#size' => 72,
'#maxlength' => 1024,
'#hidden' => TRUE,
'#states' => [
'visible' => [':input[name="vets_website_selection"]' => ['value' => 'choose']],
],
];

$form['content_release_status_block'] = $this->getContentReleaseStatusBlock();

return $form;
}

/**
* Get the rendered content release status block.
*
* @return array
* Block render array.
*/
protected function getContentReleaseStatusBlock() {
return $this->blockManager
->createInstance('content_release_status_block', [])
->build();
}

/**
* Submit the build trigger form.
*
* @param array $form
* Default form array structure.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Object containing current form state.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->submitFormForFrontend(Frontend::ContentBuild, $form_state);
$this->submitFormForFrontend(Frontend::VetsWebsite, $form_state);
parent::submitForm($form, $form_state);
}

/**
* Submit the form.
*
* @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend
* The frontend whose version we are managing.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Object containing current form state.
*/
protected function submitFormForFrontend(FrontendInterface $frontend, FormStateInterface $form_state) {
$selectionName = $frontend->getRawValue() . '_selection';
if ($form_state->getValue($selectionName) === 'default') {
$this->resetFrontendVersion($frontend, $form_state);
}
else {
$this->setFrontendVersion($frontend, $form_state);
}
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->validateFormForFrontend(Frontend::ContentBuild, $form_state);
$this->validateFormForFrontend(Frontend::VetsWebsite, $form_state);
}

/**
* Validate the form.
*
* @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend
* The frontend whose version we are managing.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Object containing current form state.
*/
protected function validateFormForFrontend(FrontendInterface $frontend, FormStateInterface $form_state) {
$selectionName = $frontend->getRawValue() . '_selection';
$gitRefName = $frontend->getRawValue() . '_git_ref';
if ($form_state->getValue($selectionName) !== 'default') {
if (empty($this->getGitRef($frontend, $form_state))) {
$form_state->setErrorByName($gitRefName, $this->t('Invalid selection.'));
}
}
}

/**
* Reset the frontend version.
*
* @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend
* The frontend whose version we are resetting.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Object containing current form state.
*/
public function resetFrontendVersion(FrontendInterface $frontend, FormStateInterface $form_state) {
if (!$this->isUnderTest($form_state)) {
$this->frontendVersion->resetVersion($frontend);
}
else {
$this->reporter->reportInfo($this->t('Reset :frontend version skipped; form is under test.', [
':frontend' => $frontend->getRawValue(),
]));
}
}

/**
* Set the frontend version according to the form.
*
* @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend
* The frontend whose version we are setting.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Object containing current form state.
*/
public function setFrontendVersion(FrontendInterface $frontend, FormStateInterface $form_state) {
if (!$this->isUnderTest($form_state)) {
$this->frontendVersion->setVersion($frontend, $this->getGitRef($frontend, $form_state));
}
else {
$this->reporter->reportInfo($this->t('Set :frontend version skipped; form is under test.', [
':frontend' => $frontend->getRawValue(),
]));
}
}

/**
* Parse a git ref out of the `git_ref` field value.
*
* @param \Drupal\va_gov_content_release\Frontend\FrontendInterface $frontend
* The frontend whose version we are setting.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Object containing current form state.
*
* @return string
* A standalone git ref, or an empty string.
*/
public function getGitRef(FrontendInterface $frontend, FormStateInterface $form_state) : string {
// If they selected a specific git ref, use that.
$gitRefName = $frontend->getRawValue() . '_git_ref';
$formValue = $form_state->getValue($gitRefName);
$result = '';
if (preg_match("/.+\\s\\(([^\\)]+)\\)/", $formValue, $matches)) {
$result = $matches[1];
}
return $result;
}

}

0 comments on commit 98a3e8b

Please sign in to comment.