You are here

public function MailchimpListsSubscription::storageSettingsForm in Mailchimp 8

Same name and namespace in other branches
  1. 2.x modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php \Drupal\mailchimp_lists\Plugin\Field\FieldType\MailchimpListsSubscription::storageSettingsForm()

Returns a form for the storage-level settings.

Invoked from \Drupal\field_ui\Form\FieldStorageConfigEditForm to allow administrators to configure storage-level settings.

Field storage might reject settings changes that affect the field storage schema if the storage already has data. When the $has_data parameter is TRUE, the form should not allow changing the settings that take part in the schema() method. It is recommended to set #access to FALSE on the corresponding elements.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

bool $has_data: TRUE if the field already has data, FALSE if not.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::storageSettingsForm

File

modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php, line 96

Class

MailchimpListsSubscription
Plugin implementation of the 'mailchimp_lists_subscription' field type.

Namespace

Drupal\mailchimp_lists\Plugin\Field\FieldType

Code

public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  $element = parent::storageSettingsForm($form, $form_state, $has_data);
  $lists = mailchimp_get_lists();
  $options = [
    '' => $this
      ->t('-- Select --'),
  ];
  foreach ($lists as $mc_list) {
    $options[$mc_list->id] = $mc_list->name;
  }
  $field_map = \Drupal::service('entity_field.manager')
    ->getFieldMap();
  $field_definitions = [];
  foreach ($field_map as $entity_type => $fields) {
    $field_definitions[$entity_type] = \Drupal::service('entity_field.manager')
      ->getFieldStorageDefinitions($entity_type);
  }

  // Prevent Mailchimp lists/audiences that have already been assigned to a
  // field appearing as field options.
  foreach ($field_map as $entity_type => $fields) {
    foreach ($fields as $field_name => $field_properties) {
      if ($field_properties['type'] == 'mailchimp_lists_subscription') {

        /* @var $field \Drupal\field\Entity\FieldStorageConfig */
        $field = $field_definitions[$entity_type][$field_name];
        $field_settings = $field
          ->getSettings();
        if ($field_name != $this
          ->getFieldDefinition()
          ->getName() && isset($field_settings['mc_list_id'])) {
          unset($options[$field_settings['mc_list_id']]);
        }
      }
    }
  }
  $refresh_lists_url = Url::fromRoute('mailchimp_lists.refresh');
  $mailchimp_url = Url::fromUri('https://admin.mailchimp.com', [
    'attributes' => [
      'target' => '_blank',
    ],
  ]);
  $element['mc_list_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Mailchimp Audience'),
    '#multiple' => FALSE,
    '#description' => $this
      ->t('Available Mailchimp audiences which are not already
        attached to Mailchimp Subscription Fields. If there are no options,
        make sure you have created an audience at @Mailchimp first, then @cacheclear.', [
      '@Mailchimp' => Link::fromTextAndUrl('Mailchimp', $mailchimp_url)
        ->toString(),
      '@cacheclear' => Link::fromTextAndUrl('clear your audience cache', $refresh_lists_url)
        ->toString(),
    ]),
    '#options' => $options,
    '#default_value' => $this
      ->getSetting('mc_list_id'),
    '#required' => TRUE,
    '#disabled' => $has_data,
  ];
  $element['double_opt_in'] = [
    '#type' => 'checkbox',
    '#title' => 'Require subscribers to Double Opt-in',
    '#description' => 'New subscribers will be sent a link with an email they must follow to confirm their subscription.',
    '#default_value' => $this
      ->getSetting('double_opt_in'),
    '#disabled' => $has_data,
  ];
  return $element;
}