Writers
On this page

Writers

This page is for the PHP developer whose platform reads a file in a format the module does not write. A writer turns a feed's rows into a file. The module registers seven formats: csv, csv_semicolon, tsv, xml (RSS 2.0), heureka_xml, json and ndjson. Once yours is registered, it is listed in the Format field of every channel that publishes a file: File, SFTP, and any file channel your own module adds; see Channels.

CSV, semicolon-separated

csv_semicolon uses Arlanix\Core\Model\Writer\SemicolonCsvWriter, which Arlanix Core registers beside csv and tsv. It writes a .csv file with media type text/csv, a header of field names and semicolons between cells. A cell containing a semicolon or a double quote is enclosed in double quotes; a double quote inside is doubled. In both CSV formats, as in TSV, line breaks in a value become spaces. Lists become comma-separated text unless the mapping uses a different separator. See Join a list.

Heureka XML

heureka_xml uses Arlanix\ProductFeed\Model\Writer\HeurekaXmlWriter. It writes a .xml file with media type application/xml, an XML 1.0 declaration in UTF-8, a SHOP root and one SHOPITEM per row. A namespace without a prefix becomes xmlns on SHOP; see Field catalogs.

Ordinary fields become elements. Lists repeat the element for each nonempty member. The writer omits text that is blank after XML cleaning and keeps numeric zero. The writer escapes XML text and removes characters XML 1.0 does not allow. It writes ordinary fields first, parameters second and deliveries last, preserving mapping order within each group.

Mapping field Output
PARAM:<name> A PARAM with PARAM_NAME from the suffix and VAL from the value. A list writes one group per value.
DELIVERY:<id> A DELIVERY with DELIVERY_ID from the suffix and DELIVERY_PRICE from the first nonempty value.
DELIVERY_COD:<id> DELIVERY_PRICE_COD inside the matching delivery, using the first nonempty value. Without a matching nonempty DELIVERY:<id>, it is silently ignored.

For ITEM_ID = sku-1, PARAM:Barva = Red, DELIVERY:DPD = 4.50 and DELIVERY_COD:DPD = 6.00, the output is:

<?xml version="1.0" encoding="UTF-8"?>
<SHOP>
  <SHOPITEM>
    <ITEM_ID>sku-1</ITEM_ID>
    <PARAM>
      <PARAM_NAME>Barva</PARAM_NAME>
      <VAL>Red</VAL>
    </PARAM>
    <DELIVERY>
      <DELIVERY_ID>DPD</DELIVERY_ID>
      <DELIVERY_PRICE>4.50</DELIVERY_PRICE>
      <DELIVERY_PRICE_COD>6.00</DELIVERY_PRICE_COD>
    </DELIVERY>
  </SHOPITEM>
</SHOP>

Ordinary field names must start with an ASCII letter or underscore and continue with ASCII letters, digits, underscores, dots or dashes. The three grouped forms require a nonempty suffix; other names fail the run with a writer error. For the store's mapping choices, see European marketplaces.

Write a writer

For a platform that asks for pipe-separated text, the following writer builds on Arlanix\Core\Model\Writer\CsvWriter, as the semicolon-separated writer does:

<?php
declare(strict_types=1);

namespace Vendor\Module\Model\Writer;

use Arlanix\Core\Model\Writer\CsvWriter;

class PipeSeparatedWriter extends CsvWriter
{
    protected const DELIMITER = '|';
    protected const EXTENSION = 'txt';
    protected const CONTENT_TYPE = 'text/plain';
    protected const LABEL = 'Pipe-separated text';
}

CsvWriter encloses a cell holding the delimiter or a double quote in double quotes and doubles a quote inside. Its base, Arlanix\Core\Model\Writer\DelimitedWriter, writes a header line of the field names, then one line per row. It turns each value into text, joining a list with commas, and replaces line breaks in a value with spaces before escapeDelimiter() protects the delimiter. For other quoting, extend DelimitedWriter and implement protected function escapeDelimiter(string $value): string.

Write any format

A format that is not one line per row implements Arlanix\Core\Model\Writer\WriterInterface:

Method Does
write(WriteInterface $stream, iterable $rows, WriterContext $context): void Writes the whole file to $stream with $stream->write(): what comes before the rows, every row in the order given, what comes after.
getExtension(): string The file extension, without the dot: csv.
getContentType(): string The media type of the file: text/csv.
getLabel(): string The format's name in the Format field: CSV, semicolon-separated.

$stream is a Magento\Framework\Filesystem\File\WriteInterface on a temporary file. Each row is an array of field name to value, and the fields come from $context->getFields(), in the order of the mapping. Use those fields even when a mapped value is null or empty. $context->getTitle() is the feed's identifier, for a format that names the file inside it, as the XML channel's <title> does. getNamespace() and getPrefix() are the feed type's XML namespace and its prefix, empty for a type that declares none; see Field catalogs.

A value can be text, a number, a boolean, null or a list: a multiselect attribute gives a list of labels, and Additional image URLs a list of addresses. Arlanix\Core\Model\Text\FlatText::of($value) gives the text a delimited format writes: '' for null or false, '1' for true, and list members joined with commas.

$rows is read once, as the rows are mapped, so that a large catalogue never sits in memory whole. Write each row as it comes; do not collect them into an array first.

To refuse to write, throw Arlanix\Core\Model\Writer\WriterException. The run fails with its message, the temporary file is discarded, and the published file stays as it was.

Register it

In your module's etc/di.xml, add the writer to WriterPool under its alias:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Arlanix\Core\Model\Writer\WriterPool">
        <arguments>
            <argument name="writers" xsi:type="array">
                <item name="pipe_delimited" xsi:type="object">Vendor\Module\Model\Writer\PipeSeparatedWriter</item>
            </argument>
        </arguments>
    </type>
</config>

The alias is what a feed stores as its format. An item under one of the module's aliases replaces the module's writer.

Then run bin/magento setup:upgrade and bin/magento cache:clean config. Open a feed with the File channel, choose Pipe-separated text in Format, save, and run Generate.

The file name and its type

For the published file name and HTTP Content-Type of your writer's format, see Where the files are.