You are here

class DateTimeFieldMapping in Webform Content Creator 3.x

Provides a datetime field mapping.

Plugin annotation


@WebformContentCreatorFieldMapping(
  id = "datetime_mapping",
  label = @Translation("Datetime"),
  weight = 0,
  field_types = {
    "datetime",
    "timestamp"
  },
)

Hierarchy

Expanded class hierarchy of DateTimeFieldMapping

File

src/Plugin/WebformContentCreator/FieldMapping/DateTimeFieldMapping.php, line 24

Namespace

Drupal\webform_content_creator\Plugin\WebformContentCreator\FieldMapping
View source
class DateTimeFieldMapping extends FieldMappingBase {
  public function getSupportedWebformFields($webform_id) {
    $supported_types = [
      "changed",
      "created",
      "date",
      "datetime",
      "datelist",
      "timestamp",
      "webform_time",
    ];
    return $this
      ->filterWebformFields($webform_id, $supported_types);
  }
  public function mapEntityField(ContentEntityInterface &$content, array $webform_element, array $data = [], FieldDefinitionInterface $field_definition) {
    $field_id = $field_definition
      ->getName();
    $field_value = $data[$field_id];
    if ($field_definition
      ->getType() == "datetime") {
      $field_data = $this
        ->convertTimestamp($field_value, $field_definition);
      $content
        ->set($field_id, $field_data);
    }
  }

  /**
   * Convert timestamp value according to field type.
   *
   * @param int $value
   *   Original datetime value.
   * @param FieldDefinitionInterface $field_definition
   *   Entity field.
   *
   * @return string
   *   Converted value.
   */
  protected function convertTimestamp($value, FieldDefinitionInterface $field_definition) {
    $date_time = new DrupalDateTime($value, 'UTC');
    $date_type = $field_definition
      ->getSettings()['datetime_type'];
    if ($date_type === 'datetime') {
      $result = \Drupal::service('date.formatter')
        ->format($date_time
        ->getTimestamp(), 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, 'UTC');
    }
    else {
      $result = \Drupal::service('date.formatter')
        ->format($date_time
        ->getTimestamp(), 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, 'UTC');
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateTimeFieldMapping::convertTimestamp protected function Convert timestamp value according to field type.
DateTimeFieldMapping::getSupportedWebformFields public function Overrides FieldMappingBase::getSupportedWebformFields
DateTimeFieldMapping::mapEntityField public function Use a single mapping to set an entity field value. Overrides FieldMappingBase::mapEntityField
FieldMappingBase::filterWebformFields protected function
FieldMappingBase::getEntityComponentFields public function Returns the entity component fields. Overrides FieldMappingInterface::getEntityComponentFields 1
FieldMappingBase::getFieldTypes public function Get the field types this plugin is available for.
FieldMappingBase::getId public function Get the plugin ID.
FieldMappingBase::getLabel public function Get the plugin label.
FieldMappingBase::getPlugin public function Return the plugin.
FieldMappingBase::getWeight public function Get the plugin weight.
FieldMappingBase::isGeneric public function Is this a generic (non-element specific) plugin.
FieldMappingBase::supportsCustomFields public function Returns whether the mapper supports custom field text Overrides FieldMappingInterface::supportsCustomFields 2
FieldMappingInterface::WEBFORM_ENTIY_REFERENCE_ELEMENTS constant
FieldMappingInterface::WEBFORM_OPTIONS_ELEMENTS constant
FieldMappingInterface::WEBFORM_TEXT_ELEMENTS constant
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 2
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. 98