DateTimeItem
class DateTimeItem extends FieldItemBase implements DateTimeItemInterface (View source)
Plugin implementation of the 'datetime' field type.
Traits
Provides dependency injection friendly methods for serialization.
Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
Wrapper methods for classes that needs typed data manager object.
Constants
| DATETIME_TYPE_DATE |
Value for the 'datetime_type' setting: store only a date. |
| DATETIME_TYPE_DATETIME |
Value for the 'datetime_type' setting: store a date and time. |
Properties
| protected array | $_serviceIds | An array of service IDs keyed by property name used for serialization. |
from DependencySerializationTrait |
| protected array | $_entityStorages | An array of entity type IDs keyed by the property name of their storages. |
from DependencySerializationTrait |
| protected TranslationInterface | $stringTranslation | The string translation service. |
from StringTranslationTrait |
| protected TypedDataManagerInterface | $typedDataManager | The typed data manager used for creating the data types. |
from TypedDataTrait |
| protected ComplexDataDefinitionInterface | $definition | The data definition. |
from Map |
| protected string | $name | The property name. |
from TypedData |
| protected TraversableTypedDataInterface|null | $parent | The parent typed data object. |
from TypedData |
| protected array | $values | An array of values for the contained properties. |
from Map |
| protected TypedDataInterface[] | $properties | The array of properties. |
from Map |
Methods
Translates a string to the current language or to a given language.
Formats a string containing a count of items.
Returns the number of plurals supported by a given language.
Gets the string translation service.
Constructs a TypedData object given its definition and context.
Constructs a TypedData object given its definition and context.
Overrides \Drupal\Core\TypedData\TypedData::setValue().
Sets the context of a property or item via a context aware parent.
Writes the value of a property without handling changes.
Gets an array of property objects.
Determines whether the data structure is empty.
React to changes to a child property or item.
Defines the storage-level settings for this plugin.
Defines the field-level settings for this plugin.
Returns the name of the main property, if any.
Returns a renderable array for a single field item.
Returns a form for the storage-level settings.
Returns a form for the field-level settings.
Returns a settings array that can be stored as a configuration value.
Returns a settings array in the field type's canonical representation.
Returns a settings array that can be stored as a configuration value.
Returns a settings array in the field type's canonical representation.
Calculates dependencies for field items.
Calculates dependencies for field items on the storage level.
Informs the plugin that a dependency of the field will be deleted.
Defines field item properties.
Details
__sleep()
{@inheritdoc}
__wakeup()
{@inheritdoc}
protected TranslatableMarkup
t(string $string, array $args = [], array $options = [])
Translates a string to the current language or to a given language.
See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.
In order for strings to be localized, make them available in one of the ways supported by the
protected
formatPlural($count, $singular, $plural, array $args = [], array $options = [])
Formats a string containing a count of items.
protected
getNumberOfPlurals($langcode = NULL)
Returns the number of plurals supported by a given language.
protected TranslationInterface
getStringTranslation()
Gets the string translation service.
$this
setStringTranslation(TranslationInterface $translation)
Sets the string translation service to use.
$this
setTypedDataManager(TypedDataManagerInterface $typed_data_manager)
Sets the typed data manager.
TypedDataManagerInterface
getTypedDataManager()
Gets the typed data manager.
static
createInstance(DataDefinitionInterface $definition, string|null $name = NULL, TraversableTypedDataInterface $parent = NULL)
Constructs a TypedData object given its definition and context.
__construct(DataDefinitionInterface $definition, string $name = NULL, TypedDataInterface $parent = NULL)
Constructs a TypedData object given its definition and context.
string
getPluginId()
Gets the plugin_id of the plugin instance.
array
getPluginDefinition()
Gets the definition of the plugin implementation.
DataDefinitionInterface
getDataDefinition()
Gets the data definition.
in
Map at line 53
mixed
getValue()
Gets the data value.
setValue(array|null $values, bool $notify = TRUE)
Overrides \Drupal\Core\TypedData\TypedData::setValue().
in
Map at line 101
string
getString()
Returns a string representation of the data.
array
getConstraints()
Gets a list of validation constraints.
ConstraintViolationListInterface
validate()
Validates the currently set data value.
in
Map at line 239
$this
applyDefaultValue(bool $notify = TRUE)
Applies the default value.
setContext(string|null $name = NULL, TraversableTypedDataInterface $parent = NULL)
Sets the context of a property or item via a context aware parent.
This method is supposed to be called by the factory only.
string|int|null
getName()
Returns the name of a property or item.
TraversableTypedDataInterface
getRoot()
Returns the root of the typed data tree.
Returns the root data for a tree of typed data objects; e.g. for an entity field item the root of the tree is its parent entity object.
string
getPropertyPath()
Returns the property path of the data.
The trail of property names relative to the root of the typed data tree, separated by dots; e.g. 'field_text.0.format'.
TraversableTypedDataInterface|null
getParent()
Returns the parent data structure; i.e. either complex data or a list.
in
Map at line 113
TypedDataInterface
get($property_name)
Gets a property object.
in
Map at line 128
$this
set($property_name, $value, bool $notify = TRUE)
Sets a property value.
protected
writePropertyValue(string $property_name, $value)
Writes the value of a property without handling changes.
Implementations of onChange() should use this method instead of set() in order to avoid onChange() being triggered again.
in
Map at line 160
TypedDataInterface[]
getProperties(bool $include_computed = FALSE)
Gets an array of property objects.
in
Map at line 173
array
toArray()
Returns an array of all property values.
Gets an array of plain property values including all not-computed properties.
in
Map at line 184
getIterator()
{@inheritdoc}
bool
isEmpty()
Determines whether the data structure is empty.
in
Map at line 212
__clone()
Magic method: Implements a deep clone.
onChange($property_name, bool $notify = TRUE)
React to changes to a child property or item.
Note that this is invoked after any changes have been applied.
static array
defaultStorageSettings()
Defines the storage-level settings for this plugin.
static array
defaultFieldSettings()
Defines the field-level settings for this plugin.
static string|null
mainPropertyName()
Returns the name of the main property, if any.
Some field items consist mainly of one main property, e.g. the value of a text field or the target_id of an entity reference. If the field item has no main property, the method returns NULL.
FieldableEntityInterface
getEntity()
Gets the entity that field belongs to.
string
getLangcode()
Gets the langcode of the field values held in the object.
FieldDefinitionInterface
getFieldDefinition()
Gets the field definition.
protected array
getSettings()
Returns the array of field settings.
protected mixed
getSetting(string $setting_name)
Returns the value of a field setting.
mixed
__get($name)
Magic method: Gets a property value.
__set($name, mixed $value)
Magic method: Sets a property value.
bool
__isset($name)
Magic method: Determines whether a property is set.
__unset($name)
Magic method: Unsets a property.
array
view(string|array $display_options = [])
Returns a renderable array for a single field item.
preSave()
Defines custom presave behavior for field values.
This method is called during the process of saving an entity, just before values are written into storage. When storing a new entity, its identifier will not be available yet. This should be used to massage item property values or perform any other operation that needs to happen before values are stored. For instance this is the proper phase to auto-create a new entity for an entity reference field item, because this way it will be possible to store the referenced entity identifier.
bool
postSave(bool $update)
Defines custom post-save behavior for field values.
This method is called during the process of saving an entity, just after values are written into storage. This is useful mostly when the business logic to be implemented always requires the entity identifier, even when storing a new entity. For instance, when implementing circular entity references, the referenced entity will be created on pre-save with a dummy value for the referring entity identifier, which will be updated with the actual one on post-save.
In the rare cases where item properties depend on the entity identifier, massaging logic will have to be implemented on post-save and returning TRUE will allow them to be rewritten to the storage with the updated values.
delete()
Defines custom delete behavior for field values.
This method is called during the process of deleting an entity, just before values are deleted from storage.
static array
generateSampleValue(FieldDefinitionInterface $field_definition)
Generates placeholder field values.
Useful when populating site with placeholder content during site building or profiling.
deleteRevision()
Defines custom revision delete behavior for field values.
This method is called from during the process of deleting an entity revision, just before the field values are deleted from storage. It is only called for entity types that support revisioning.
array
storageSettingsForm(array $form, FormStateInterface $form_state, bool $has_data)
Returns a form for the storage-level settings.
Invoked from \Drupal\field_ui\Form\FieldStorageConfigEditForm to allow administrators to configure storage-level settings.
Field storage might reject settings changes that affect the field storage schema if the storage already has data. When the $has_data parameter is TRUE, the form should not allow changing the settings that take part in the schema() method. It is recommended to set #access to FALSE on the corresponding elements.
array
fieldSettingsForm(array $form, FormStateInterface $form_state)
Returns a form for the field-level settings.
Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.
static array
storageSettingsToConfigData(array $settings)
Returns a settings array that can be stored as a configuration value.
For all use cases where field settings are stored and managed as configuration, this method is used to map from the field type's representation of its settings to a representation compatible with deployable configuration. This includes:
- Array keys at any depth must not contain a ".".
- Ideally, array keys at any depth are either numeric or can be enumerated as a "mapping" within the configuration schema. While not strictly required, this simplifies configuration translation UIs, configuration migrations between Drupal versions, and other use cases.
- To support configuration deployments, references to content entities must use UUIDs rather than local IDs.
An example of a conversion between representations might be an "allowed_values" setting that's structured by the field type as a \Drupal\Core\TypedData\OptionsProviderInterface::getPossibleOptions() result (i.e., values as keys and labels as values). For such a use case, in order to comply with the above, this method could convert that representation to a numerically indexed array whose values are sub-arrays with the schema definable keys of "value" and "label".
static array
storageSettingsFromConfigData(array $settings)
Returns a settings array in the field type's canonical representation.
This function does the inverse of static::storageSettingsToConfigData(). It's called when loading a field's settings from a configuration object.
static array
fieldSettingsToConfigData(array $settings)
Returns a settings array that can be stored as a configuration value.
Same as static::storageSettingsToConfigData(), but for the field's settings.
static array
fieldSettingsFromConfigData(array $settings)
Returns a settings array in the field type's canonical representation.
This function does the inverse of static::fieldSettingsToConfigData(). It's called when loading a field's settings from a configuration object.
static array
calculateDependencies(FieldDefinitionInterface $field_definition)
Calculates dependencies for field items.
Dependencies are saved in the field configuration entity and are used to determine configuration synchronization order. For example, if the field type's default value is a content entity, this method should return an array of dependencies listing the content entities.
static array
calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition)
Calculates dependencies for field items on the storage level.
Dependencies are saved in the field storage configuration entity and are used to determine configuration synchronization order. For example, if the field type storage depends on a particular entity type, this method should return an array of dependencies listing the module that provides the entity type.
Dependencies returned from this method are stored in field storage configuration and are always considered hard dependencies. If the dependency is removed the field storage configuration must be deleted.
static bool
onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies)
Informs the plugin that a dependency of the field will be deleted.
static DataDefinitionInterface[]
propertyDefinitions(FieldStorageDefinitionInterface $field_definition)
Defines field item properties.
Properties that are required to constitute a valid, non-empty item should be denoted with \Drupal\Core\TypedData\DataDefinition::setRequired().
static array
schema(FieldStorageDefinitionInterface $field_definition)
Returns the schema for the field.
This method is static because the field schema information is needed on creation of the field. FieldItemInterface objects instantiated at that time are not reliable as field settings might be missing.
Computed fields having no schema should return an empty array.