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

  1. 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;
      }
    }
    
  2. Create a new event series.
  3. Edit the series. Note that the recurrence type field is now disabled.
  4. 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.

Command icon 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

muriqui created an issue. See original summary.

owenbush’s picture

Thanks for raising the issue. I am currently working on a fix which I hope to have ready to review tomorrow.

owenbush’s picture

Status: Active » Needs review

I 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.

muriqui’s picture

Status: Needs review » Reviewed & tested by the community

Hey, thanks for the quick turnaround! Yes, this does seem to resolve the issue.

muriqui’s picture

Version: 2.0.0-rc3 » 2.0.x-dev
StatusFileSize
new15.16 KB
new14.88 KB

Backported patches for 2.0.0-rc2 and 2.0.0-rc3, for anyone who needs them.

muriqui’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new15.43 KB
new15.16 KB
new17.44 KB

Updated the MR and patch files to fix a bug in EventCreationService::getDatesFromForm() where it was still expecting the ->getUserInput() data.

  • owenbush committed 8228800 on 2.0.x
    Issue #3271328 by muriqui, owenbush: Disabling event series date fields...

  • owenbush committed 2be1c8b on 8.x-1.x
    Issue #3271328 by muriqui, owenbush: Disabling event series date fields...
owenbush’s picture

Status: Needs review » Fixed

Thank 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

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.