Skip to content

Instantly share code, notes, and snippets.

@junaidpv
Last active September 26, 2023 18:44
Show Gist options
  • Save junaidpv/baa227c6ca58e87184611060a581ab9d to your computer and use it in GitHub Desktop.
Save junaidpv/baa227c6ca58e87184611060a581ab9d to your computer and use it in GitHub Desktop.
Provide configuration settings for MailChimp Sign Up blocks to customize displaying of fields.
diff --git a/modules/mailchimp_signup/src/Plugin/Block/MailchimpSignupSubscribeBlock.php b/modules/mailchimp_signup/src/Plugin/Block/MailchimpSignupSubscribeBlock.php
index 1798344..3ff60c9 100644
--- a/modules/mailchimp_signup/src/Plugin/Block/MailchimpSignupSubscribeBlock.php
+++ b/modules/mailchimp_signup/src/Plugin/Block/MailchimpSignupSubscribeBlock.php
@@ -9,6 +9,8 @@ use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\mailchimp_signup\Entity\MailchimpSignup;
use Symfony\Component\DependencyInjection\ContainerInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Component\Utility\NestedArray;
/**
* Provides a 'Subscribe' block.
@@ -93,6 +95,27 @@ class MailchimpSignupSubscribeBlock extends BlockBase implements ContainerFactor
$content = $this->formBuilder->getForm($form);
+ foreach ($this->configuration['mergefield_settings'] as $field => $settings) {
+ if (empty($content['mergevars'][$field])) {
+ continue;
+ }
+ $content['mergevars'][$field]['#weight'] = $settings['weight'];
+ $content['mergevars'][$field]['#title_display'] = $settings['label_display'] ?? ($content['mergevars'][$field]['#title_display'] ?? 'before');
+
+ $attributes = static::getAttributesFromConfig($settings['other_attributes'] ?? '');
+ if (!empty(trim($settings['classes']))) {
+ $attributes['class'] = [trim($settings['classes'])];
+ }
+ $content['mergevars'][$field]['#attributes'] = NestedArray::mergeDeep($content['mergevars'][$field]['#attributes'] ?? [], $attributes);
+
+ if (!empty(trim($settings['wrapper_classes']))) {
+ if (!isset($content['mergevars'][$field]['#wrapper_attributes']['class'])) {
+ $content['mergevars'][$field]['#wrapper_attributes']['class'] = [];
+ }
+ $content['mergevars'][$field]['#wrapper_attributes']['class'][] = trim($settings['wrapper_classes']);
+ }
+ }
+
return $content;
}
@@ -125,4 +148,147 @@ class MailchimpSignupSubscribeBlock extends BlockBase implements ContainerFactor
return $id;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration() {
+ return [
+ 'mergefield_settings' => [],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function blockForm($form, FormStateInterface $form_state) {
+ $signup_id = $this->getDerivativeId();
+ /* @var $signup \Drupal\mailchimp_signup\Entity\MailchimpSignup */
+ $signup = mailchimp_signup_load($signup_id);
+
+ $table = [
+ '#type' => 'table',
+ '#title' => $this->t('Merge Field Settings'),
+ '#caption' => $this->t('You may customize order of merge fields and provide additional classes.'),
+ '#header' => [
+ $this->t('Field'),
+ $this->t('Weight'),
+ $this->t('Label Display'),
+ $this->t('Field Classes'),
+ $this->t('Wrapper Classes'),
+ $this->t('Other attributes'),
+ ],
+ '#regions' => [],
+ '#attributes' => [
+ 'class' => ['field-ui-overview'],
+ 'id' => 'field-display-overview',
+ ],
+ '#tabledrag' => [
+ [
+ 'action' => 'order',
+ 'relationship' => 'sibling',
+ 'group' => 'field-order',
+ ],
+ ],
+ ];
+
+ foreach ($signup->mergefields as $mergefield => $enabled) {
+ if (!$enabled) {
+ continue;
+ }
+
+ $settings = $this->configuration['mergefield_settings'][$mergefield] ?? [];
+
+ $table[$mergefield]['field'] = [
+ '#type' => 'markup',
+ '#markup' => $mergefield,
+ ];
+
+ $table[$mergefield]['#attributes']['class'] = ['draggable'];
+ $table[$mergefield]['weight'] = [
+ '#type' => 'weight',
+ '#title' => t('Weight'),
+ '#title_display' => 'invisible',
+ '#default_value' => $settings['weight'] ?? 0,
+ '#attributes' => [
+ 'class' => [
+ 'field-order'
+ ]
+ ],
+ ];
+
+ $table[$mergefield]['label_display'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Label Display'),
+ '#title_display' => 'invisible',
+ '#options' => [
+ 'before' => $this->t('Before'),
+ 'after' => $this->t('After'),
+ 'invisible' => $this->t('Invisible'),
+ ],
+ '#default_value' => $settings['label_display'] ?? 'before',
+ ];
+
+ $table[$mergefield]['classes'] = [
+ '#type' => 'textfield',
+ '#title' => $this->t('Classes'),
+ '#title_display' => 'invisible',
+ '#default_value' => $settings['classes'] ?? '',
+ ];
+ $table[$mergefield]['wrapper_classes'] = [
+ '#type' => 'textfield',
+ '#title' => $this->t('Wrapper Classes'),
+ '#title_display' => 'invisible',
+ '#default_value' => $settings['wrapper_classes'] ?? '',
+ ];
+
+ $table[$mergefield]['other_attributes'] = [
+ '#type' => 'textarea',
+ '#title' => $this->t('Other Attributes'),
+ '#title_display' => 'invisible',
+ '#default_value' => $settings['other_attributes'] ?? '',
+ '#description' => $this->t('Set attributes for this wrapper. Enter one value per line, in the format attribute|value. The value is optional. Tokens are supported in attribute values.'),
+ ];
+ }
+
+ $form['mergefield_settings'] = $table;
+
+ return $form;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function blockSubmit($form, FormStateInterface $form_state) {
+ $this->configuration['mergefield_settings'] = $form_state->getValue('mergefield_settings');
+ }
+
+ /**
+ * Convert multiline configuration for HTML attributes to array
+ *
+ * @param $attributes_string string
+ * @return array
+ */
+ public static function getAttributesFromConfig($attributes_string) {
+ // Split by new line.
+ $attribute_list = explode("\n", $attributes_string);
+ // Trim white spaces.
+ $attribute_list = array_map('trim', $attribute_list);
+ // Remove empty lines.
+ $attribute_list = array_filter($attribute_list, 'strlen');
+
+ $attributes = [];
+ foreach ($attribute_list as $text) {
+ // Check for an explicit key.
+ $matches = [];
+ if (preg_match('/^([\w]+(\-[\w]+)*)\|(.*)$/', $text, $matches)) {
+ // Trim key and value to avoid unwanted spaces issues.
+ $key = trim($matches[1]);
+ $value = trim($matches[3]);
+
+ $attributes[$key] = \Drupal::token()->replace($value, [], ['clear' => TRUE]);
+ }
+ }
+ return $attributes;
+ }
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment