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

dixon_’s picture

Issue summary: View changes

Updated issue summary.

dixon_’s picture

Just updated the OP to reflect what me, @fago and @effulgentsia talked about in the coder lounge just now.

dixon_’s picture

Issue summary: View changes

Updated issue summary.

dixon_’s picture

Issue summary: View changes

Updated issue summary.

fago’s picture

Another resource for primitives used by others:
http://sdl.ikayzo.org/display/SDL/Language+Guide

dixon_’s picture

Assigned: Unassigned » dixon_

I'll take care of this on my flight.

fago’s picture

We should ensure that all primitives are idempotent, i.e. if you do:

$value = $item->value;
$item->value = $value;

$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?

Jose Reyero’s picture

I 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?

fago’s picture

Assigned: dixon_ » fago

There 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.

fago’s picture

Issue summary: View changes

Updated issue summary.

fago’s picture

Issue summary: View changes

added overview

fago’s picture

Issue summary: View changes

added timestamp to date

fago’s picture

Issue summary: View changes

added some describing text

fago’s picture

I've started implementing everything such that it matches the table as in the summary - will continue tomorrow.

fago’s picture

Status: Active » Needs review

I've done so and pushed it to my branch - please take a look. I've also completed the tests.

fago’s picture

Status: Needs review » Fixed

I'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().

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

renamed to builtin