When extending the MediaEntityGeneratorD7 we don't have access to vid in the $parent_iterator

Created on 17 November 2023, 7 months ago

Problem/Motivation

I'm extending the MediaEntityGeneratorD7 in order to add thumbnail_file_path and thumbnail_file_name to the files_found array.

protected function initializeIterator() {

    $query_files = $this->select('file_managed', 'f')
      ->fields('f')
      ->condition('uri', 'temporary://%', 'NOT LIKE')
      ->orderBy('f.timestamp');

    $all_files = $query_files->execute()->fetchAllAssoc('fid');

    $files_found = [];

    foreach ($this->sourceFields as $name => $source_field) {

      $parent_iterator = parent::initializeIterator();

      foreach ($parent_iterator as $entity) {
        $nid = $entity['nid'];
        $vid = $entity['vid'];
        $langcode = $this->configuration['langcode'] ?? NULL;
        $field_value = $this->getFieldValues('node', $name, $nid, $vid, $langcode);

        foreach ($field_value as $reference) {
          // Add support to thumbnail files.
          if (isset($reference['thumbnail']) && !empty($all_files[$reference['thumbnail']]['uri'])) {

            $thumbnail_file_url = $all_files[$reference['thumbnail']]['uri'];
            if (!empty($this->configuration['d7_file_url'])) {
              $thumbnail_file_url = str_replace('public://', '', $thumbnail_file_url);
              $file_path = UrlHelper::encodePath($thumbnail_file_url);
              $thumbnail_file_url = $this->configuration['d7_thumbnail_file_url'] . $file_path;
            }

            // Make sure the file name is correct based on the file url.
            $thumb_file_name = $all_files[$reference['thumbnail']]['filename'];
            $file_url_pieces = explode('/', $thumbnail_file_url);
            if ($thumb_file_name !== end($file_url_pieces)) {
              $thumb_file_name = end($file_url_pieces);
            }

            $files_found[$reference['fid']] = $entity;
            $files_found[$reference['fid']]['thumbnail_file_path'] = $thumbnail_file_url;
            $files_found[$reference['fid']]['thumbnail_file_name'] = $thumb_file_name;
          }
        }
      }
    }
    return new \ArrayIterator($files_found);
  }

However, $entity['vid'] is not set.

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡·Brazil carolpettirossi Campinas - SP

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024