Hi, I ran into an issue where mailchimp_lists_load_email fails to load the email address when the email field is stored against a related entity.

For example, instead of applying Mailchimp Subscription field to the user profile directly, it's instead applied to an entity which references the user profile. I can configure/save this without any issues, but when I try and display the field an error occurs as the field cannot be found.

This seems to happen as the $mail_property variable contains a path to the referenced entity (eg 'uid:entity:mail') which can't be handled when calling $entity::get (on line 118 of mailchimp_lists.module).

My quick/dirty hack is to check if the property contains a colon character, and if so manually traverses the object graph (see attached patch), but I'm sure there's probably a better way to handle this.

Let me know if you need any further info.

Comments

JeremySkinner created an issue. See original summary.

yanniboi’s picture

Status: Active » Needs review

Yup, I can confirm this issue. I think ideally we want to have a default way for traversing entity relationships (such as entity reference fields) similar to \Drupal\Core\Entity\Query\Sql\Table::addField() (https://www.drupal.org/node/2825947). I don't know off the top of my head if there is already a way of doing this in Drupal core, but I feel like there should be.

yanniboi’s picture

StatusFileSize
new1.12 KB

Hmmm there is something wrong with the formating of that patch. It doesnt apply for me.

Uploading it again.

samuel.mortenson’s picture

Assigned: Unassigned » rjacobsen0
rjacobsen0’s picture

StatusFileSize
new4.86 KB

It's less a hack if the code for dereferencing the entity is next to the code that builds the list of options. I put them both in class MailchimpListsSubscription. I also ran into a bug where I was able to delete a field and because it was the chosen email an error was thrown. Catching it and assigning null in that case.

samuel.mortenson’s picture

Status: Needs review » Needs work

Lots of review, mostly coding standard related:

  1. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -358,24 +358,10 @@ class MailchimpListsSubscription extends FieldItemBase {
    +          $options = $this->getOptionsForSubEntity($required, $field_definition, $target_definition, $target_type, $keypath, $label, $options);
    

    I think we should either rename this method to "addOptionsForSubEntity" and have it take in $options as a reference, or keep the name but instead of being passed options, have it return FALSE if no bundle could be found, or the options to be appended to $options.

  2. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +    /**
    +     * @param $required
    +     * @param $field_definition
    +     * @param $target_definition
    +     * @param $target_type
    +     * @param $keypath
    +     * @param $label
    +     * @param $options
    +     * @return mixed
    +     */
    

    Doc here needs fleshed out.

  3. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +    {
    

    Per Drupal coding standards, opening curly braces should be on the same line as the function/method declaration. Also all the new code should use two spaces, not four.

  4. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +        // this is a recursive call (calls the method that called the current method)
    

    Per coding standards "Inline comments must start with a capital letter". Also should be less than 80 characters.

  5. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +        $options[(string)$label] = $this->getFieldmapOptions($field_definition->getSetting('target_type'), $bundle, $required, $keypath . ':entity', $label);
    

    Need a space between (string) and $label here.

  6. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +
    +
    

    Only one blank space between methods is required/standard.

  7. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +    /**
    +     * @param $entity
    +     * @return \Drupal\Core\Field\FieldItemList
    +     */
    

    Doc needs fleshed out here.

  8. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +    public function GetMailField($entity)
    +    {
    

    The curly brace needs moved, the method name should start with a lowercase letter, and the method should be private.

  9. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +      // Get the entity's email property value from the user-defined email field.
    

    Just barely over 80 characters here.

  10. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +        /* @var $mail_field \Drupal\Core\Field\FieldItemList */
    ...
    +        /* @var $mail_field \Drupal\Core\Field\FieldItemList */
    

    It looks like my IDE is reading this docblock correctly, but typically the variable name comes after the class name - i.e. "@var \Foo $foo"

  11. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +          $mail_field = $mail_field->$accessor;
    

    Should we be doing a try/catch here as well? $accessor isn't necessarily set on $entity.

  12. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +      } else {
    

    Drupal coding standards have "else" always appearing on its own line.

  13. +++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
    @@ -398,4 +384,61 @@ class MailchimpListsSubscription extends FieldItemBase {
    +          $mail_field = null;
    

    In the Drupal coding standards FALSE, TRUE, and NULL are always uppercase. Also we should make sure to update the docblock to reflect that NULL can be returned from this method.

samuel.mortenson’s picture

Assigned: rjacobsen0 » aprice42
enrega’s picture

The patch in #5 doesn't apply to the current dev version of the module. Can the patch be updated please?

gcb’s picture

Component: Lists » Lists Module
aprice42’s picture

Rerolled patch and made requested changes.

aprice42’s picture

Reverting the change to making getMailField private, as it needs to be public for this usage.

aprice42’s picture

+++ b/modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
@@ -401,4 +392,83 @@ class MailchimpListsSubscription extends FieldItemBase {
+      $accessors = explode(':', $mail_property);
+      /* @var $mail_field \Drupal\Core\Field\FieldItemList */
+      $mail_field = $entity;
+      foreach ($accessors as $accessor) {
+        try {
+          $mail_field = $mail_field->$accessor;
+        }
+        catch (\Exception $e) {
+          $mail_field = NULL;
+        }
+      }

There is something broken here. Not totally clear about what is supposed to happen, but it definitely feels like setting $mail_field to the $entity value, and then changing that value in the foreach is creating one issue, but if this was working, then I am confused how/why it was.

mrdalesmith’s picture

Hit this issue and the latest patch resolves it for me.

reinfate’s picture

The last patch produces a warning if one of the field ancestors is NULL. For example, if the entity is only being created and the reference field is not set yet.
Fixed it by adding a simple check at the line 136 in the patch

aprice42’s picture

Assigned: aprice42 » Unassigned

  • xenophyle committed 455b0208 on 8.x-1.x
    Issue #2949125 by aprice42, JeremySkinner, yanniboi, rjacobsen0,...

  • xenophyle committed 608cec58 on 2.x
    Issue #2949125 by aprice42, JeremySkinner, yanniboi, rjacobsen0,...

xenophyle’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.