Any plans to devel a drupal 8 version? If not, there are any alternative to this module on D8?

Comments

thiagomoraesp created an issue. See original summary.

Tourista’s picture

D8 port would me appreciated !

rooby’s picture

I think it would be best to get the D7 version working properly first.

Primarily that would be this issue: #2244833: Image Grabber doesn't work with latest version of Feeds

rooby’s picture

Category: Feature request » Task
rooby’s picture

Priority: Major » Normal
cosolom’s picture

I can suggest using a custom tamper plugin for this (for converting url to image fid)

<?php


namespace Drupal\YOU_MODULE_NAME\Plugin\Tamper;


use Drupal;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tamper\Exception\TamperException;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;

/**
 * Plugin implementation for replace url to image with file fid.
 *
 * @Tamper(
 *   id = "image_url_to_fid",
 *   label = @Translation("Image url to fid"),
 *   description = @Translation("Download image, store it in folder and return fid."),
 *   category = "Files"
 * )
 */
class ImageUrlToFid extends TamperBase {
  
  const SETTING_PATH = 'image_path';
  
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $config = parent::defaultConfiguration();
    $config[self::SETTING_PATH] = 'public://[date:custom:Y]-[date:custom:m]';
    return $config;
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form[self::SETTING_PATH] = [
      '#type' => 'textfield',
      '#title' => $this->t('Path for store images'),
      '#default_value' => $this->getSetting(self::SETTING_PATH),
      '#description' => $this->t('The target path where images will be saved.'),
    ];
    
    return $form;
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->setConfiguration([
      self::SETTING_PATH => $form_state->getValue(self::SETTING_PATH),
    ]);
  }
  
  /**
   * Tamper data.
   *
   * Performs the operations on the data to transform it.
   *
   * @param mixed $data
   *   The data to tamper.
   * @param \Drupal\tamper\TamperableItemInterface $item
   *   Item that can be tampered as part of a plugin's execution.
   *
   * @return mixed
   *   The tampered data.
   *
   * @throws \Drupal\tamper\Exception\TamperException
   *   When the plugin can not tamper the given data.
   * @throws \Drupal\tamper\Exception\SkipTamperDataException
   *   When the calling tamper process should be skipped for the given data.
   * @throws \Drupal\tamper\Exception\SkipTamperItemException
   *   When the calling tamper process should be skipped for the given item.
   */
  public function tamper($data, TamperableItemInterface $item = NULL) {
    if (!UrlHelper::isValid($data)) {
      throw new TamperException('Url should be valid.');
    }
    $token = Drupal::service('token');
    $file_system = Drupal::service('file_system');
    $dest_dir = $this->getSetting(self::SETTING_PATH);
    $dest_dir = $token->replace($dest_dir);
    if ($file_system->prepareDirectory($dest_dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
      $parse = parse_url($data);
      $info = pathinfo($parse['path']);
      $file_name = $info['basename'];
      
      $image_url = $data;
      $file_name = $file_system->createFilename($file_name, $dest_dir);
    
      if ($file = system_retrieve_file($image_url, $file_name, TRUE, FILE_EXISTS_RENAME)) {
        return $file->id();
      }
      else {
        throw new TamperException('Error downloading image file');
      }
    }
    else {
      throw new TamperException('Can not create directory for store images');
    }
    
  }
}
hansrossel’s picture

Great! Thanks for the code example of the tamper plugin. As long as feeds for D8 does not do this out of the box, tampering seems indeed the way to go.

I have tested it but get "The specified plugin is invalid" when I activate it. Does it need some code in tamper.schema.yml?

I'm also thinking this proposal for this conversion should maybe be in the main feeds or tamper issue queue to get more visibility.

hansrossel’s picture

With the patch in this issue for Feeds you can just use remote absolute urls of your images and files in your csv and get them imported into file-ID.

cosolom’s picture

This is just an example. You need to modify it according to your requirements (create a module, replace YOU_MODULE_NAME with the name of your module, put the file in the /src/Plugin/Tamper directory, ...) Also this code creates duplicate files if you run feed for the second time.
Thanks for the link to the feed's patch. Native solution is always better.

jidrone’s picture

Hi everyone,

I have been working in a module able to create a media element from the href attributes, it provides a Drupal Action and a feed tamper.

I think it is still on beta, but you can start trying the dev release on https://www.drupal.org/project/html_processors.

bogdog400’s picture

@jidrone -- Thanks for working on a new module and posting here.

webengr’s picture

recommendation for a Drupal 11 replacement to feeds image grabber ?