Managed file uploads

Next, we are going to look at how we can work with managed files using a custom form element. And to demonstrate this, we are finally going to create another Product importer plugin. This time, instead of a remote JSON resource, we will allow users to upload a CSV file that contains product data and imports that into Product entities. This is what the example CSV data looks like:

id,name,number 
1,Car,45345 
2,Motorbike,54534  

It basically has the same kind of data as the JSON resource we've been looking at so far but without the image reference. So let's get going with our new plugin class.

Here is our starting point:

namespace DrupalproductsPluginImporter; 
 
use DrupalCoreStringTranslationStringTranslationTrait; 
   use DrupalproductsPluginImporterBase; 
 
/** 
 * Product importer from a CSV format. 
 * 
 * @Importer( 
 *   id = "csv", 
 *   label = @Translation("CSV Importer") 
 * ) 
 */ 
class CsvImporter extends ImporterBase { 
 
  use StringTranslationTrait; 
 
  /** 
   * {@inheritdoc} 
   */ 
  public function import() { 
    $products = $this->getData(); 
    if (!$products) { 
      return FALSE; 
    } 
 
    foreach ($products as $product) { 
      $this->persistProduct($product); 
    } 
 
    return TRUE; 
  } 
}  

We start by extending from the ImporterBase class and implement the obligatory import() method. Like before, we delegate to getData() to retrieve the product information, but in this case we simply loop over the resulting records and use the persistProduct() method to save the Product entities. So no batch operations. Apart from no longer saving images, this latter method looks exactly like the one from the JsonImporter, so I won't be copying it over again. But it makes for a good homework assignment to try to move it to the base class and abstract away the dynamic portions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset