You are here

public function MailchimpListsSubscription::postSave 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::postSave()

Defines custom post-save behavior for field values.

This method is called during the process of saving an entity, just after values are written into storage. This is useful mostly when the business logic to be implemented always requires the entity identifier, even when storing a new entity. For instance, when implementing circular entity references, the referenced entity will be created on pre-save with a dummy value for the referring entity identifier, which will be updated with the actual one on post-save.

In the rare cases where item properties depend on the entity identifier, massaging logic will have to be implemented on post-save and returning TRUE will allow them to be rewritten to the storage with the updated values.

Parameters

bool $update: Specifies whether the entity is being updated or created.

Return value

bool Whether field items should be rewritten to the storage as a consequence of the logic implemented by the custom behavior.

Overrides FieldItemBase::postSave

File

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

Class

MailchimpListsSubscription
Plugin implementation of the 'mailchimp_lists_subscription' field type.

Namespace

Drupal\mailchimp_lists\Plugin\Field\FieldType

Code

public function postSave($update) {
  parent::postSave($update);
  $choices = $this->values;

  // Only act if the field has a value to prevent unintended unsubscription.
  if (!empty($choices)) {
    $field_settings = $this->definition
      ->getSettings();

    // Automatically subscribe if the field is configured to hide the
    // Subscribe checkbox and at least one interest group checkbox is checked.
    if ($field_settings['show_interest_groups'] && $field_settings['hide_subscribe_checkbox']) {
      if (!empty($choices['interest_groups'])) {
        $subscribe_from_interest_groups = FALSE;
        foreach ($choices['interest_groups'] as $group_id => $interests) {
          foreach ($interests as $interest_id => $value) {
            if (!empty($value)) {
              $subscribe_from_interest_groups = TRUE;
              continue;
            }
          }
        }
        $choices['subscribe'] = $subscribe_from_interest_groups;
      }
    }
    mailchimp_lists_process_subscribe_form_choices($choices, $this, $this
      ->getEntity());
  }
}