SendinBlue is a third party newsletter subscription service that integrates with Drupal 8 CMS, Unfortunately there is no direct way of referencing any of the SendinBlue subscription forms as referenced entities in node or any other entity, this blog post will guide the developers to reference these type of forms through Paragraphs.

I am assuming that following steps have already been completed before following the instructions, these includes:

  • Setting up an account on SendinBlue and setting up subscription list
  • Get the API key from SendinBlue and configure it in SendinBlue-Drupal Configurations. 
  • Familiar about Paragraphs and how to create them and attach it to node.

Create a SendinBlue entity, You can create as many form as you like, just note that each form will refer to single subscription list. After you create your entity (form) you will see a list like this.

SendinBlue Entity overview page
SendinBlue Entity overview page 

The fun part starts here..., Create a Paragraph type lets call it "Form", In this paragraph type, I have created two fields; Background Image and SendinBlue entity reference that is referencing "SendinBlue Signup Form Entity".

 

Paragraph Manage Field

The field "field_sendinblue_form" is attached to paragraph type "form", in "Manage form display" this field has been setup to have select widget. Below is a code to alter the option list for this select widget. 

function [YOURMODULE]_options_list_alter(array &$options, array $context) {

  // Check if this is the field we want to change.
  if ($context['fieldDefinition']
    ->id() == 'paragraph.form.field_sendinblue_form') {
    foreach ($options as $id => $value) {
      if (is_numeric($id)) {
        $connection = \Drupal::database();
        $query = $connection->query("SELECT title FROM {sendinblue_signup} WHERE mcsId = :mcsId", [
          ':mcsId' => $id,
        ]);
        $options[$id] = $query->fetchField();
      }
    }
  }
}

When creating an entity, in the background, the option_list_alter will alter options and show real form titles that we used above when creating SendinBlue form entity. 

SendinBlue form select-list in entity

This is to display the form through preprocess functions, here I have explained it using paragraphs, if needed one can also use the code in preprocess_node if the field is attached to node entity.

function [YOURTHEME]_preprocess_paragraph__form(&$variables) {

  // Grab signup form
  $id = $paragraph->get("field_sendinblue_form")->getValue()[0]['target_id'];

  $connection = \Drupal::database();
  $query = $connection->query("SELECT title FROM {sendinblue_signup} WHERE mcsId = :mcsId", [
    ':mcsId' => $id,
  ]);
  $form_title = $query->fetchField();

  $form = new SubscribeForm($id);

  $form_markup = \Drupal::formBuilder()->getForm($form);

  $form_markup['description']['#markup'] = "<h2>$form_title</h2>" . $form_markup['description']['#markup'];

}