Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,15 @@ public function getForm($data = [], $loadData = true)

$record->id = $id;

// Remove trashed option from state field for new article form
if ($id == 0) {
$field = $form->getField('state');

if ($field !== false && $field->type === 'List') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do an instanceof check as the type field can be overwritten and then you can get a field with the type list but it is actually not a ListField class. Probably only on rare cases, but better safe than sorry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I'm considering it now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laoneo I changed it, I hope I have your feedback.

$field->removeOption(-2);
}
}

// For new articles we load the potential state + associations
if ($id == 0 && $formField = $form->getField('catid')) {
$assignedCatids = $data['catid'] ?? $form->getValue('catid');
Expand Down
21 changes: 21 additions & 0 deletions libraries/src/Form/Field/ListField.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ public function addOption($text, $attributes = [])
return $this;
}

/**
* Method to remove an option from list field.
*
* @param string $value The value of the option to remove
*
* @return static For chaining.
*
* @since __DEPLOY_VERSION__
*/
public function removeOption(string $value): static
{
foreach ($this->element->option as $option) {
if ((string) $option['value'] === $value) {
$dom = dom_import_simplexml($option);
$dom->parentNode->removeChild($dom);
}
}

return $this;
}

/**
* Method to get certain otherwise inaccessible properties from the form field object.
*
Expand Down