Problem/Motivation
An event series' Recur Type field or the associated date fields can't be disabled via hook_form_alter() without triggering an exception on save.
For example, I am trying to alter the series edit form to prevent users from changing the recurrence pattern (and thus deleting/recreating the instances) when additional data has been attached to one of the instances. If I alter the form to disable the recur_type field, then edit and save an event series, the EventCreationService throws an exception.
Steps to reproduce
- In a custom module, implement the following:
use Drupal\Core\Form\FormStateInterface; function my_module_form_eventseries_form_alter(&$form, FormStateInterface $form_state, $form_id) { $entity = $form_state->getFormObject()->getEntity(); if (!$entity->isNew()) { $form['recur_type']['#disabled'] = TRUE; } } - Create a new event series.
- Edit the series. Note that the recurrence type field is now disabled.
- Click Save.
Expected result: The event series is saved, keeping its current recurrence type field value.
Actual result: EventCreationService::convertFormConfigToArray() throws an exception.
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "" plugin does not exist. Valid plugin IDs for Drupal\Core\Field\FieldTypePluginManager are: comment, datetime, daterange, file_uri, file, image, link, list_float, list_string, list_integer, path, consecutive_recurring_date, daily_recurring_date, monthly_recurring_date, weekly_recurring_date, text_with_summary, text, text_long, integer, uri, entity_reference, uuid, float, email, timestamp, string, language, created, changed, map, string_long, password, decimal, boolean in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 53 of core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php).
Drupal\Core\Plugin\DefaultPluginManager->getDefinition(NULL) (Line: 276)
Drupal\recurring_events\EventCreationService->convertFormConfigToArray(Object) (Line: 159)
Proposed resolution
The problem seems to be on line 225 of EventCreationService.php, where it is using $form_state->getUserInput() rather than $form_state->getValues() to access the submitted form values. The difference is that disabled form fields are not available in getUserInput(), whereas getValues() includes the existing values for any disabled fields.
The solution would be to refactor EventCreationService::convertFormConfigToArray() to use getValues().
Remaining tasks
The proposed resolution would also require some additional processing to transform the data returned by getValues() into the same format as the raw values returned by getUserInput(), so that the rest of convertFormConfigToArray() can continue to function as it does currently.
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | recurring_events-3271328-7.patch | 17.44 KB | muriqui |
| #7 | recurring_events-3271328-7-reroll-rc3.patch | 15.16 KB | muriqui |
| #7 | recurring_events-3271328-7-reroll-rc2.patch | 15.43 KB | muriqui |
Issue fork recurring_events-3271328
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
owenbush commentedThanks for raising the issue. I am currently working on a fix which I hope to have ready to review tomorrow.
Comment #4
owenbush commentedI have opened an MR with the changes to fix this bug. Please let me know if you can check it out and confirm it works.
Comment #5
muriqui commentedHey, thanks for the quick turnaround! Yes, this does seem to resolve the issue.
Comment #6
muriqui commentedBackported patches for 2.0.0-rc2 and 2.0.0-rc3, for anyone who needs them.
Comment #7
muriqui commentedUpdated the MR and patch files to fix a bug in EventCreationService::getDatesFromForm() where it was still expecting the ->getUserInput() data.
Comment #10
owenbush commentedThank you for the update to the MR and the additional patches for the other RC releases, that's really helpful I appreciate that a lot.
I have gone ahead and merged this into the dev branch for 8.x-1.x and 2.0.x