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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\office_hours\Plugin\migrate\process\OfficeHoursField
- class \Drupal\office_hours\Plugin\migrate\process\CSVOfficeHoursField
- class \Drupal\office_hours\Plugin\migrate\process\OfficeHoursField
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\processView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CSVOfficeHoursField:: |
public | function |
Performs the associated process. Overrides OfficeHoursField:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |