Problem/Motivation

I would like to merge in core examples of Typed Data from Typed Example. Maybe as typeddata_example instead of typed_example?

Proposed resolution

Pull out core stuff into its own module and place into examples

Remaining tasks

See #2209627: [meta] Module Checklist for Examples for a full list:

  • Inline code comments to describe data definition and data type
  • Doc block and group comments to describe high-level about classes
  • Add a description page. Describe what type of example this is (it's a back end code example without any UI).
  • Needs an actual readme
  • Language needs to change to be more beginner-friendly

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mradcliffe created an issue. See original summary.

mradcliffe’s picture

Status: Needs review » Needs work

The last submitted patch, 2: examples-2808785-add-typed-data-example-2.patch, failed testing.

mradcliffe’s picture

Status: Needs review » Needs work

The last submitted patch, 4: examples-2808785-add-typed-data-example-4.patch, failed testing.

mradcliffe’s picture

I guess not. I wonder why it's not finding the classes.

blakehall’s picture

Status: Needs work » Reviewed & tested by the community

This example looks good to me, and once in should allow for some refactoring of the field_example RGB field to use this new Typed Data. I'm assuming that refactoring is beyond the scope of this issue but I'm marking it as Reviewed & Tested since it looks like a solid example to me.

If there is anything I can do to help move the consideration of this patch along please let me know.

Mile23’s picture

Status: Reviewed & tested by the community » Needs work

Would be nice to get the tests passing...

At a glance I noticed there's no functional tests of paths created for the example, because there aren't any paths. So we need a description page you can read when it's installed.

Other things to add at the checklist here: #2209627: [meta] Module Checklist for Examples

Also needs a ton of inline comments. For instance, this docblock should explain what all the annotation is doing:

+++ b/typed_data_example/src/Plugin/DataType/Color.php
@@ -0,0 +1,49 @@
+/**
+ * @DataType(
+ *   id = "typed_data_example_color",
+ *   label = @Translation("Color"),
+ *   definition_class = "\Drupal\typed_data_example\TypedData\ColorDefinition",
+ *   list_class = "\Drupal\typed_data_example\Plugin\DataType\ExampleColorItemList"
+ * )
+ */
+class Color extends Map {
mradcliffe’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
3.46 KB
16.68 KB

Agreed.

diff --git a/typed_data_example/src/TypedData/ColorDefinition.php b/typed_data_example/src/TypedData/ColorDefinition.php
index 4ece8b3..fe36182 100644
--- a/typed_data_example/src/TypedData/ColorDefinition.php
+++ b/typed_data_example/src/TypedData/ColorDefinition.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\typed_example\TypedData;
+namespace Drupal\typed_data_example\TypedData;
 
 use Drupal\Core\TypedData\ComplexDataDefinitionBase;
 use Drupal\Core\TypedData\DataDefinition;

I'm not sure how I missed this those many months ago, but it was glaringly obvious to me this afternoon.

Hopefully this patch addresses the unit test failures and some code standard fixes. This should flip back to Needs Work if it passes.

mradcliffe’s picture

Status: Needs review » Needs work

Yep, that was it. Back to Needs work.

mradcliffe’s picture

I added a description page for the module with some content.

Still needed to be done:

  • Functional test for the description page
  • Better documentation on the description page?
  • Also I think that an example of setComputed is necessary. Someone asked about that on IRC and that is the only page that we do not have on the Typed Data API docs pages. Working on an example for that will also lead directly to documentation improvements there too.
Mile23’s picture

Assigned: mradcliffe » Unassigned
Status: Needs work » Needs review

Patch still applies.. Running the testbot.

jungle’s picture

Version: 8.x-1.x-dev » 3.x-dev

The default branch is 3.x-dev which is Drupal 9 compatible. And I would port this to Drupal 9. :)

jungle’s picture

jungle’s picture

Testing on HEAD is broken. Fixing in #3160118: Fix broken testing on HEAD

jungle’s picture

>Rerolled the patch for 3.x, changes explaining in next comment.

Interrupted in the middle. Forgot...

  1. Moved the module into the subfolder modules
  2. +++ b/typed_data_example/typed_data_example.info.yml
    @@ -0,0 +1,7 @@
    +core: 8.x
    

    one line change made for D9

  3. +++ b/typed_data_example/typed_data_example.routing.yml
    @@ -0,0 +1,9 @@
    +    _admin_route: TRUE
    ...
    +    _access: TRUE
    

    TRUE should be 'TRUE'

  4. HEAD is GREEN again, see https://www.drupal.org/pift-ci-job/1763454
jungle’s picture

Fix failed tests in #14 and a little improvements below-relevant.

+++ b/modules/typed_data_example/tests/src/Unit/TypedData/ColorDefinitionTest.php
@@ -0,0 +1,30 @@
+   * Assert that properties exist.

Should start with "Tests"

jungle’s picture

jungle’s picture

FileSize
21.92 KB
3.72 KB
diff --git a/modules/typed_data_example/tests/src/Unit/Plugin/DataType/ColorSetTest.php b/modules/typed_data_example/tests/src/Unit/Plugin/DataType/ColorSetTest.php
index 878650a..dffdfc7 100644
--- a/modules/typed_data_example/tests/src/Unit/Plugin/DataType/ColorSetTest.php
+++ b/modules/typed_data_example/tests/src/Unit/Plugin/DataType/ColorSetTest.php
@@ -12,7 +12,7 @@ use Drupal\typed_data_example\TypedData\ColorSetDefinition;
 use Prophecy\Argument;
 
 /**
- * Test the ColorSet data type.
+ * Tests the ColorSet data type.
  *
  * @ingroup typed_data_example
  *
@@ -26,7 +26,7 @@ class ColorSetTest extends UnitTestCase {
   /**
    * {@inheritdoc}
    */
-  protected function setUp() {
+  protected function setUp(): void {
 
     $definition = ColorSetDefinition::create('typed_data_example_color_set');
     $definition->setClass('\Drupal\typed_data_example\Plugin\DataType\ColorSet');
@@ -76,7 +76,7 @@ class ColorSetTest extends UnitTestCase {
   }
 
   /**
-   * Assert that the data type class is returned when getting property.
+   * Tests that the data type class is returned when getting property.
    *
    * @param string $class_name
    *   The class name to test.
@@ -90,7 +90,7 @@ class ColorSetTest extends UnitTestCase {
   }
 
   /**
-   * Get the test parameters for testProperty.
+   * Data provider for testProperty().
    *
    * @return array
    *   An array of test parameters.
diff --git a/modules/typed_data_example/tests/src/Unit/Plugin/DataType/ColorTest.php b/modules/typed_data_example/tests/src/Unit/Plugin/DataType/ColorTest.php
index 8c74114..55c289a 100644
--- a/modules/typed_data_example/tests/src/Unit/Plugin/DataType/ColorTest.php
+++ b/modules/typed_data_example/tests/src/Unit/Plugin/DataType/ColorTest.php
@@ -30,7 +30,7 @@ class ColorTest extends UnitTestCase {
   /**
    * {@inheritdoc}
    */
-  protected function setUp() {
+  protected function setUp(): void {
 
     // The color definition class is fairly safe to use without mocking.
     $definition = ColorDefinition::create('typed_data_example_color');
diff --git a/modules/typed_data_example/tests/src/Unit/TypedData/ColorSetDefinitionTest.php b/modules/typed_data_example/tests/src/Unit/TypedData/ColorSetDefinitionTest.php
index e0a27af..2c1ccf5 100644
--- a/modules/typed_data_example/tests/src/Unit/TypedData/ColorSetDefinitionTest.php
+++ b/modules/typed_data_example/tests/src/Unit/TypedData/ColorSetDefinitionTest.php
@@ -7,7 +7,7 @@ use Drupal\Tests\UnitTestCase;
 use Drupal\typed_data_example\TypedData\ColorSetDefinition;
 
 /**
- * Test the Example definition.
+ * Tests the ColorSet definition.
  *
  * @ingroup typed_data_example
  *
@@ -19,7 +19,7 @@ class ColorSetDefinitionTest extends UnitTestCase {
   /**
    * {@inheritdoc}
    */
-  protected function setUp() {
+  protected function setUp(): void {
     $typedDataProphecy = $this->prophesize('\Drupal\Core\TypedData\TypedDataManagerInterface');
     $container = new ContainerBuilder();
     $container->set('typed_data_manager', $typedDataProphecy->reveal());
@@ -27,7 +27,7 @@ class ColorSetDefinitionTest extends UnitTestCase {
   }
 
   /**
-   * Assert that properties exist.
+   * Tests that properties exist.
    */
   public function testGetPropertyDefinitions() {
     $definition = new ColorSetDefinition();
diff --git a/modules/typed_data_example/typed_data_example.links.menu.yml b/modules/typed_data_example/typed_data_example.links.menu.yml
index e305d2b..554fa75 100644
--- a/modules/typed_data_example/typed_data_example.links.menu.yml
+++ b/modules/typed_data_example/typed_data_example.links.menu.yml
@@ -1,3 +1,3 @@
 typed_data_example.description:
   title: Typed Data Example
-  route_name: typed_data_example_description
\ No newline at end of file
+  route_name: typed_data_example_description

A few improvements

  1. Adding newline at the end of file to typed_data_example.links.menu.yml
  2. Adding type hinting to setUp().
  3. Tests comments start with "verb".
jungle’s picture

Tagging Needs issue summary update for what left to do here or leaving all the rest to separate issues.

valthebald’s picture

@jungle thanks for pushing this example forward!
I'm still looking at the code (it's a big patch, sorry!) Meanwhile, I suggest to use more specific file name for the template - `description.html.twig` would be hard to find in a real theme. Something like `typed-data-description.html.twig` maybe?

valthebald’s picture

Status: Needs review » Needs work

Other comments:

- it's not clear how to use procedural code from typed_data_example.module. Should this code be moved to a service?
- return values of procedural functions are not used
- description.html.twig is not used anywhere (or I missed it?)
- changes suggested in #19 (defining void return type) are relevant only with PHP7.4, and should be fixed in core classes too, without it static analysis will continue to fail