Score:0

Feeds event subscriber stopped working

cn flag

For some reason my feeds event subscribers have just stopped working.

My code looks like this, the dpm for debugging never gets fired so I think the function isn't being run.

I can't work out what's changed.


namespace Drupal\partshub_feeds_modifications\EventSubscriber;

use Drupal\feeds\Event\EntityEvent;
use Drupal\feeds\Event\FeedsEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Reacts to feeds events to modify feed properties.
 */
class FeedsSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[FeedsEvents::PROCESS_ENTITY_PRESAVE][] = 'presave';
    $events[FeedsEvents::PROCESS_ENTITY_POSTSAVE][] = 'postsave';

    return $events;
  }

  /**
   * Acts on presaving an entity.
   *
   * @param Drupal\feeds\Event\EntityEvent $event
   *   The feed event.
   */
  public function presave(EntityEvent $event) {
    dpm($event);
    // Get the feed object.
    $feed = $event->getFeed();
    // Get the entity being operated on.
    if ($feed->type->entity->id() === 'parts') {
      $entity = $event->getEntity();
      // Get the unchanged entity.
      $unchanged_entity = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadUnchanged($entity->id());
      // Check if the entity is new.
      if (!$entity->isNew()) {
        // Only if part is not new check values.
        if ($feed->type->entity->label() === 'Parts') {
          if ($entity->get('title')->value === 'empty') {
            $entity->get('title')->value = $unchanged_entity->get('title')->value;
          }

          // Part fitment/makes field.
          if ($entity->get('field_make')->isEmpty()) {
            $makes = $unchanged_entity->get('field_make')->referencedEntities();
            foreach ($makes as $index => $term) {
              $tid = $term->id();
              if ($index == 0) {
                $entity->set('field_make', $tid);
              }
              else {
                $entity->get('field_make')->appendItem([
                  'target_id' => $tid,
                ]);
              }
            }
          }


          // Part group code.
          if ($entity->get('field_group_code')->isEmpty()) {
            $codes = $unchanged_entity->get('field_group_code')
              ->referencedEntities();
            foreach ($codes as $index => $code) {
              $code_id = $code->id();
              if ($index == 0) {
                $entity->set('field_group_code', $code_id);
              }
              else {
                $entity->get('field_group_code')->appendItem([
                  'target_id' => $code_id,
                ]);
              }
            }
          }

          // Cross references.
          if ($entity->get('field_cross_references')->isEmpty()) {
            $references = $unchanged_entity->get('field_cross_references')
              ->referencedEntities();
            foreach ($references as $index => $reference) {
              $reference_id = $reference->id();
              if ($index == 0) {
                $entity->set('field_cross_references', $reference_id);
              }
              else {
                $entity->get('field_cross_references')->appendItem([
                  'target_id' => $reference_id,
                ]);
              }
            }
          }
        }
        elseif ($feed->type->entity->label() === 'Physical attributes') {
          if ($entity->get('title')->value === 'empty') {
            $entity->get('title')->value = $unchanged_entity->get('title')->value;
          }
          // Part weight.
          if ($entity->get('field_weight')->isEmpty()) {
            $entity->get('field_weight')->value = $unchanged_entity->get('field_weight')->value;
          }
          // Part Height.
          if ($entity->get('field_height')->isEmpty()) {
            $entity->get('field_height')->value = $unchanged_entity->get('field_height')->value;
          }
          // Part Width.
          if ($entity->get('field_width')->isEmpty()) {
            $entity->get('field_width')->value = $unchanged_entity->get('field_width')->value;
          }
          // Part Depth.
          if ($entity->get('field_depth')->isEmpty()) {
            $entity->get('field_depth')->value = $unchanged_entity->get('field_depth')->value;
          }
        }
      }
    }
  }

  /**
   * Acts on presaving an entity.
   *
   * @param Drupal\feeds\Event\EntityEvent $event
   *   The feed event.
   */
  public function postsave(EntityEvent $event) {
    $feed = $event->getFeed();
    $feeds = ['oe_references', 'sub_components', 'dimensions'];
    $id = $feed->type->entity->id();
    if (in_array($id, $feeds)) {
      $entity = $event->getEntity();
      $parent_field_name = $entity->parent_field_name->value;
      $parent_id = $entity->parent_id->value;
      $vid = \Drupal::entityTypeManager()->getStorage('node')->getLatestRevisionId($parent_id);
      $part = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($vid);
      $paragraph_references = $part->get($parent_field_name)->referencedEntities();
      $paragraph_references_id = [];
      if ($paragraph_references) {
        foreach ($paragraph_references as $paragraph_reference) {
          $paragraph_references_id[] = $paragraph_reference->id();
        }
      }
      if (!in_array($entity->id(), $paragraph_references_id)) {
        $fields = [
          'field_oe_references', 'field_sub_components_', 'field_dimensions'
        ];
        if (in_array($parent_field_name, $fields)) {
          if ($part) {
            $part->{$parent_field_name}[] = $entity;
            $part->save();
          }
        }
      }
    }
  }

}

I also have a service definition as follows.

services:
  # Name of this service.
  parthsub_feeds_modifications_events_subscriber:
    # Event subscriber class that will listen for the events.
    class: Drupal\partshub_feeds_modifications\EventSubscriber\FeedsSubscriber
    # Tagged as an event_subscriber to register this subscriber with the event_dispatch service.
    tags:
      - { name: event_subscriber }
cn flag
The custom module with this code is enabled, right?
cn flag
yeah :-) so strange as it all worked before, checked all the basics cleared cache etc. I had a look at some examples and I've seen some service names with . in so I've also tried changing the service name to `parthsub_feeds_modifications.events_subscriber` but that doesn't work either.
Score:0
cn flag

No answer to this, I was getting validation errors before the presave was fired which meant this code never fired.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.