You are here

class CSVOfficeHoursField in Office Hours 8

Processes a input array of office hours to the correct format for the field.

The concat CSVOfficeHours is used to generate a well formed array of opening hours for use in the Office hours field.

Available configuration keys:

  • slots_per_day: (optional) The time slots per day, defaults to 1.
  • delimiter: (optional) Your time slots should be in the following format: 00:00 - 00:00: Two times separated by a character. With this option you can set this delimiter, defaults to '-'.
  • comment: (optional) do we have to import comments for each slot ? Defaults to FALSE.

Examples:

Example 1 without comment:


process:
  new_office_hours_field:
    plugin: csv_office_hours
    slots_per_day: 2
    delimiter: '-'
    source:
       - 'Sunday 1'
       - 'Sunday 2'
       - 'Monday 1'
       - 'Monday 2'
       - 'Tuesday 1'
       - 'Tuesday 2'
       - 'Wednesday 1'
       - 'Wednesday 2'
       - 'Thursday 1'
       - 'Thursday 2'
       - 'Friday 1'
       - 'Friday 2'
       - 'Saturday 1'
       - 'Saturday 2'

Example 2 with comment:


process:
  new_office_hours_field:
    plugin: csv_office_hours
    slots_per_day: 2
    delimiter: '-'
    comment: true
    source:
       - 'Sunday 1'
       - 'Sunday 1 comment'
       - 'Sunday 2'
       - 'Sunday 2 comment'
       - 'Monday 1'
       - 'Monday 1 comment'
       - 'Monday 2'
       - 'Monday 2 comment'
       - 'Tuesday 1'
       - 'Tuesday 1 comment'
       - 'Tuesday 2'
       - 'Tuesday 2 comment'
       - 'Wednesday 1'
       - 'Wednesday 1 comment'
       - 'Wednesday 2'
       - 'Wednesday 2 comment'
       - 'Thursday 1'
       - 'Thursday 1 comment'
       - 'Thursday 2'
       - 'Thursday 2 comment'
       - 'Friday 1'
       - 'Friday 1 comment'
       - 'Friday 2'
       - 'Friday 2 comment'
       - 'Saturday 1'
       - 'Saturday 1 comment'
       - 'Saturday 2'
       - 'Saturday 2 comment'

This will import to a field with two time slots set per day.

Plugin annotation


@MigrateProcessPlugin(
  id = "csv_office_hours"
)

Hierarchy

Expanded class hierarchy of CSVOfficeHoursField

See also

\Drupal\migrate\Plugin\MigrateProcessInterface

File

src/Plugin/migrate/process/CSVOfficeHoursField.php, line 96

Namespace

Drupal\office_hours\Plugin\migrate\process
View source
class CSVOfficeHoursField extends OfficeHoursField {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (!is_array($value)) {
      throw new MigrateException(sprintf('%s is not an array', var_export($value, TRUE)));
    }
    $slots_per_day = isset($this->configuration['slots_per_day']) ? $this->configuration['slots_per_day'] : 1;
    $delimiter = isset($this->configuration['delimiter']) ? $this->configuration['delimiter'] : '-';
    $has_comment = isset($this->configuration['comment']) ? $this->configuration['comment'] : FALSE;
    if (count($value) !== $slots_per_day * ($has_comment ? 2 : 1) * 7) {
      throw new MigrateException(sprintf('%s does not have the correct size', var_export($value, TRUE)));
    }
    $office_hours = [];
    for ($i = 0; $i < count($value); $i++) {
      if (!$has_comment || $i % 2 === 0) {

        // Process Hours.
        $time = explode($delimiter, trim($value[$i]));
        $office_hours[] = [
          'day' => floor($i / $slots_per_day / ($has_comment ? 2 : 1)),
          'starthours' => str_replace(':', '', $time[0]),
          'endhours' => str_replace(':', '', $time[1]),
          'comment' => '',
        ];
      }
      else {
        $comment_key = ($i - 1) / 2;
        $item =& $office_hours[$comment_key];
        $item['comment'] = trim($value[$i]);

        // Override empty values because it is not well handled if there
        // is a comment associated with a day. But if there is no comment
        // it has to be empty.
        if (!empty($item['comment'])) {
          $item['starthours'] = empty($item['starthours']) ? -1 : $item['starthours'];
          $item['endhours'] = empty($item['endhours']) ? -1 : $item['endhours'];
        }
      }
    }
    return $office_hours;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CSVOfficeHoursField::transform public function Performs the associated process. Overrides OfficeHoursField::transform
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.