I am using Migrate CSV to migrate sharepoint nodes from a csv file into Drupal 8
I need to strip part of the page titles. An example page title is about.aspx. I want to strip the .aspx
Right now I am doing a straight mapping like this= title: title. Is there a plugin that can strip this out?
I know I could do a search and replace in the csv file but I feel its safer to remove in the processing.
I have looked here but cannot find one.
https://www.drupal.org/docs/8/api/migrate-api/migrate-process/
also is there a plugin that can add content. Or Prefix and Suffix. Example is the body section I am mapping over straight has no html. I would like to Prefix and Suffix the paragraph tag <p>my body section needs html tags</p>

Comments

J.R.’s picture

I used the callback plugin for simple conversion by calling into PHP.

I am still a PHP beginner so I don't know if there is something like "remove extension".

However, writing your own process plugin is not too difficult:
here is an example which transforms a date (I guess PHP has
the primitives you need to find a '.' from behind and cut
the string up to that position .. in fact it's basically what I did,
but for a very different purpose .. you may prefer to change
the name of the plugin, though :-) ..).

<?php

/**
 * @file
 * Contains \Drupal\migrate_ning2\Plugin\migrate\process\RoundToSeconds.
 */

namespace Drupal\migrate_ning2\Plugin\migrate\process;

use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateSkipRowException;

/**
 * Round time to seconds by removing suffix .<millisecs>Z
 *
 * @MigrateProcessPlugin(
 *   id = "round_to_seconds"
 * )
 */
class RoundToSeconds extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    // $value: cut off .<nnn>Z 
    if( is_null($value) )
    {
      return $value;
    }

    // $old = $value;

    $pos = strpos($value,'.');
    if( FALSE !== $pos )
    {
      $value = substr($value,0,$pos); 
    }

    // drush_print_r( $old . ' -> ' . $value );
    
    return $value;
  }

}

If you want to see the complete example
(module which contains the plugin):
I published it as GPL: https://www.drupal.org/node/2810683