- Issue created by @dan_metille
When rendering the Simplenews subscription block programmatically (e.g., using {{ drupal_block('simplenews_subscription_block') }} in Twig Tweak or custom code), the following exception occurs:
Exception: Unique ID must be set with setUniqueId. in Drupal\simplenews\Form\SubscriptionsBlockForm->getFormId()
This happens because the block configuration's unique_id is empty by default, and the block's build() method does not generate one unless the block is placed and configured via the UI.
The block should auto-generate a unique ID if none is set, so it works out-of-the-box in programmatic or Twig Tweak use.
Update SimplenewsSubscriptionBlock::build() to generate a UUID if unique_id is empty, e.g.:
$unique_id = !empty($this->configuration['unique_id']) ? $this->configuration['unique_id'] : $this->uuid->generate();
$form_object = $this->entityTypeManager->getFormObject('simplenews_subscriber', 'block')
->setUniqueId($unique_id)
// ...
Manually supply a unique ID in the Twig Tweak call:
{{ drupal_block('simplenews_subscription_block', { unique_id: 'footer_newsletter' }) }}
Or place the block via the UI.
A patch is attached that implements the fix.
Active
4.1
Code