"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/migrate/src/Plugin/migrate/process/Concat.php" (26 Nov 2020, 1960 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Concat.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\migrate\Plugin\migrate\process;
    4 
    5 use Drupal\migrate\MigrateException;
    6 use Drupal\migrate\MigrateExecutableInterface;
    7 use Drupal\migrate\ProcessPluginBase;
    8 use Drupal\migrate\Row;
    9 
   10 /**
   11  * Concatenates a set of strings.
   12  *
   13  * The concat plugin is used to concatenate strings. For example, imploding a
   14  * set of strings into a single string.
   15  *
   16  * Available configuration keys:
   17  * - delimiter: (optional) A delimiter, or glue string, to insert between the
   18  *   strings.
   19  *
   20  * Examples:
   21  *
   22  * @code
   23  * process:
   24  *   new_text_field:
   25  *     plugin: concat
   26  *     source:
   27  *       - foo
   28  *       - bar
   29  * @endcode
   30  *
   31  * This will set new_text_field to the concatenation of the 'foo' and 'bar'
   32  * source values. For example, if the 'foo' property is "Rosa" and the 'bar'
   33  * property is "Parks", new_text_field will be "RosaParks".
   34  *
   35  * You can also specify a delimiter.
   36  *
   37  * @code
   38  * process:
   39  *   new_text_field:
   40  *     plugin: concat
   41  *     source:
   42  *       - foo
   43  *       - bar
   44  *     delimiter: /
   45  * @endcode
   46  *
   47  * This will set new_text_field to the concatenation of the 'foo' source value,
   48  * the delimiter and the 'bar' source value. For example, using the values above
   49  * and "/" as the delimiter, if the 'foo' property is "Rosa" and the 'bar'
   50  * property is "Rosa", new_text_field will be "Rosa/Parks".
   51  *
   52  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
   53  *
   54  * @MigrateProcessPlugin(
   55  *   id = "concat",
   56  *   handle_multiples = TRUE
   57  * )
   58  */
   59 class Concat extends ProcessPluginBase {
   60 
   61   /**
   62    * {@inheritdoc}
   63    */
   64   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
   65     if (is_array($value)) {
   66       $delimiter = isset($this->configuration['delimiter']) ? $this->configuration['delimiter'] : '';
   67       return implode($delimiter, $value);
   68     }
   69     else {
   70       throw new MigrateException(sprintf('%s is not an array', var_export($value, TRUE)));
   71     }
   72   }
   73 
   74 }