Original Issue
Revisit the current list of primitive data types to be complete and ensure they map well to http://phpcr.github.com/doc/html/phpcr/propertytype.html
We decided at DrupalCon that we should mark all data types with appropriate constants. The constants would live in a separate class Drupal\Core\TypedData\DataPrimitive
.
So hook_data_type_info()
would work something like this:
use Drupal\Core\TypedData;
/**
* Implements hook_data_type_info().
*/
function system_data_type_info() {
return array(
'string' => array(
/* ... */
'primitive type' => DataPrimitive::TYPE_STRING,
),
);
}
Proposed solution
The TypedData API data model consists of data lists, data structures and data primitives, whereas a list is a sequential list of data and a structure contains any number of data properties. Data definitions describe data based upon defined data types, which are primitives and any module added data type (as e.g. a field type or an entity). Added data types may be mapped to the built-in primitives which are predefined, e.g. an e-mail type would be mapped to the string primitive (but could add further validation logic). This allows modules to operate with any kind of typed data just by supporting data structures, data lists and the built-in primitives.
Built-in primitive data types
Data type | Machine name | Plain value | Further value formats (may be used when setting) |
---|---|---|---|
String | string | PHP string | any PHP variable that casts well to a string |
Boolean | boolean | PHP boolean | any PHP variable that casts well to a boolean |
Integer | integer | PHP integer | any PHP variable that casts well to an integer |
Decimal | decimal | PHP float | any PHP variable that casts well to a float |
Date | date | DateTime object | any string supported by DateTime::__construct(), or a timestamp as integer |
Duration | duration | DateInterval object | a ISO8601 string as supported by DateInterval::__construct, or an integer in seconds |
URI | uri | PHP string (absolute URI) | - |
Binary | binary | PHP resource | absolute stream resource URI as string |
Data definitions relate to a class which may be used to wrap and work with the data value based upon the DataWrapperInterface
. Further typed data wrappers can be derived from objects implementing the DataStructureInterface
or the DataListInterface
.
Comments
Comment #0.0
dixon_Updated issue summary.
Comment #1
dixon_Just updated the OP to reflect what me, @fago and @effulgentsia talked about in the coder lounge just now.
Comment #1.0
dixon_Updated issue summary.
Comment #1.1
dixon_Updated issue summary.
Comment #2
fagoAnother resource for primitives used by others:
http://sdl.ikayzo.org/display/SDL/Language+Guide
Comment #3
dixon_I'll take care of this on my flight.
Comment #4
fagoWe should ensure that all primitives are idempotent, i.e. if you do:
$item->value should still be the same. When looking at the binary primitive, I noticed it isn't as the get-value is the stream and the set value the uri - so it violates this rule. Maybe we could just use the stream resource uri as value and add another method for making use of it?
Or should binary just be a $data variable holding binary data, while getting the value from stream wrappers could be done via a computed property?
Comment #5
Jose Reyero CreditAttribution: Jose Reyero commentedI am looking for more information on that hook_data_type_info(), as it can be useful for other parts like #1648930: Introduce configuration schema and use for translation
Is there any documentation / patch around which defines these data types?
Comment #6
fagoThere is no good summary or documentation yet, however constants for primitives are defined at the DataPrimitive class. I'll work on compiling a overview of primitives, hook docs and valid definitions per data type.
I'll post my proposed overview to the issue summary.
Comment #6.0
fagoUpdated issue summary.
Comment #6.1
fagoadded overview
Comment #6.2
fagoadded timestamp to date
Comment #6.3
fagoadded some describing text
Comment #7
fagoI've started implementing everything such that it matches the table as in the summary - will continue tomorrow.
Comment #8
fagoI've done so and pushed it to my branch - please take a look. I've also completed the tests.
Comment #9
fagoI've added in the respective docs of supported plain values to the implementation classes as well. I've also added docs now, see http://drupal.org/node/1732754#comment-6428976 and drupal_wrap_data().
Comment #10.0
(not verified) CreditAttribution: commentedrenamed to builtin