Sub-issue for meta issue #1880976: [meta] Port examples (including submodules) to D9.4+

Problem/Motivation

We need to demonstrate adding contextual links to entities.

Proposed resolution

Let's add a new module, called contextual_link_example, which implements edit, view, and delete contextual links to the content_entity_example module.

This module will have examples:content_entity_example as a dependency.

CommentFileSizeAuthor
#47 interdiff.txt3.76 KBMile23
#47 2102647_46.patch26.69 KBMile23
#45 interdiff-43-45.txt969 bytesnavneet0693
#45 2102647-45.patch23.99 KBnavneet0693
#43 interdiff-2102647-41-43.txt3.53 KBmartin107
#43 2102647-43.patch23.89 KBmartin107
#41 2102647-41.patch23.87 KBLal_
#39 2102647-39.patch23.82 KBLal_
#35 interdiff.txt7.43 KBLal_
#35 2102647-32.patch26.22 KBLal_
#31 2102647-31.patch21.45 KBnavneet0693
#30 interdiff-2102647-29-30.txt1.27 KBmartin107
#30 trait-2102647-30.patch21.56 KBmartin107
#29 examples-contextual-links-2102647-29.patch21.46 KBnavneet0693
#29 interdiff-25-29.txt3.88 KBnavneet0693
#28 Contextual Exampels JS test HTML output.png656.88 KBnavneet0693
#25 examples-contextual-links-2102647-25.patch21.17 KBnavneet0693
#25 interdiff-23-25.txt3.75 KBnavneet0693
#23 examples-contextual-links-2102647-23.patch21.63 KBnavneet0693
#23 interdiff-18-23.txt4.58 KBnavneet0693
#18 examples-contextual-links-2102647-18.patch21.48 KBnavneet0693
#18 interdiff-12-18.txt1.98 KBnavneet0693
#17 Contextual Example Block.png123.24 KBnavneet0693
#12 interdiff-10-12.txt2.15 KBnavneet0693
#12 examples-contextual-links-2102647-12.patch21.41 KBnavneet0693
#10 examples-contextual-links-2102647-10.patch21.72 KBnavneet0693
#10 interdiff-8-10.txt15.82 KBnavneet0693
#8 examples-contextual-links-2102647-8.patch25.01 KBnavneet0693
#7 2102647-7.patch22.09 KBLal_
#4 examples-port-contextual-links-2102647-4.patch32.64 KBicanblink
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

a_thakur’s picture

Assigned: Unassigned » a_thakur
Issue summary: View changes

Started working on this.

marvil07’s picture

Assigned: a_thakur » Unassigned

Just cleaning. If there is a patch please add it.

icanblink’s picture

Assigned: Unassigned » icanblink

Looking into it.

icanblink’s picture

Added the patch for porting contextual links example.

icanblink’s picture

Assigned: icanblink » Unassigned
Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 4: examples-port-contextual-links-2102647-4.patch, failed testing.

Lal_’s picture

I've rerolled the patch.

navneet0693’s picture

Status: Needs work » Needs review
FileSize
25.01 KB

Added tests and did a significant amount changes to patch #7, added theme functions, removed unwanted code, corrected code and now it's fully working !

Status: Needs review » Needs work

The last submitted patch, 8: examples-contextual-links-2102647-8.patch, failed testing. View results

navneet0693’s picture

Status: Needs work » Needs review
FileSize
15.82 KB
21.72 KB

Status: Needs review » Needs work

The last submitted patch, 10: examples-contextual-links-2102647-10.patch, failed testing. View results

navneet0693’s picture

Status: Needs work » Needs review
FileSize
21.41 KB
2.15 KB

Status: Needs review » Needs work

The last submitted patch, 12: examples-contextual-links-2102647-12.patch, failed testing. View results

Mile23’s picture

Thanks!

Here's some review. More to come on the test.

  1. +++ b/contextual_links_example/contextual_links_example.info.yml
    @@ -0,0 +1,7 @@
    +dependencies:
    +  - drupal:examples
    

    Should require contextual.

  2. +++ b/contextual_links_example/src/Entity/CLEEntity.php
    @@ -0,0 +1,48 @@
    + *   handlers = {
    + *     "storage" = "Drupal\contextual_links_example\Storage\CLEStorage"
    + *   },
    
    +++ b/contextual_links_example/src/Storage/CLEStorage.php
    @@ -0,0 +1,117 @@
    +class CLEStorage implements EntityStorageInterface {
    ...
    +    // TODO: Implement resetCache() method.
    ...
    +    // TODO: Implement loadMultiple() method.
    ...
    +    // TODO: Implement load() method.
    ...
    +    // TODO: Implement loadUnchanged() method.
    ...
    +    // TODO: Implement loadRevision() method.
    ...
    +    // TODO: Implement deleteRevision() method.
    ...
    +    // TODO: Implement loadByProperties() method.
    ...
    +    // TODO: Implement create() method.
    ...
    +    // TODO: Implement delete() method.
    ...
    +    // TODO: Implement save() method.
    ...
    +    // TODO: Implement getQuery() method.
    ...
    +    // TODO: Implement getAggregateQuery() method.
    ...
    +    // TODO: Implement getEntityTypeId() method.
    ...
    +    // TODO: Implement getEntityType() method.
    

    Why do we have a storage class if it's just a stub implementation?

  3. +++ b/contextual_links_example/tests/src/Functional/ContextualLinksExampleTest.php
    @@ -0,0 +1,79 @@
    +  public static $modules = ['contextual', 'contextual_links_example'];
    

    Should not require contextual here.

Mile23’s picture

  1. +++ b/contextual_links_example/src/Controller/CLEController.php
    @@ -0,0 +1,55 @@
    +  public function cleContent(int $entity_id) {
    

    Can't typehint int for PHP 5.5.

  2. +++ b/contextual_links_example/src/Plugin/Block/CLEBlock.php
    @@ -0,0 +1,40 @@
    +class CLEBlock extends BlockBase {
    

    This block never shows up in the block admin page.

As for the tests.... Based on Drupal\Tests\contextual\FunctionalJavascript\ContextualLinksTest, it seems we'll need to inherit from JavascriptTestBase to get the click that opens the contextual menu.

That needs some local dev environment setup: https://www.drupal.org/docs/8/phpunit/phpunit-javascript-testing-tutorial

navneet0693’s picture

Hey @Mile23, thank you very much your review.

Yes, Storage class is just a stub implementation.

I will work upon for other changes.

navneet0693’s picture

FileSize
123.24 KB

@Mile23

+++ b/contextual_links_example/src/Plugin/Block/CLEBlock.php
@@ -0,0 +1,40 @@
+class CLEBlock extends BlockBase {
This block never shows up in the block admin page.

This does show up. You will have to place it using Block Interface UI.

navneet0693’s picture

navneet0693’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 18: examples-contextual-links-2102647-18.patch, failed testing. View results

navneet0693’s picture

I am doing something wrong here,

@Mil23, after observing html results of tests, I observed that contextual links doesn't render on minimal profile . I having less knowledge on how to proceed in this scenario. How can we proceed with tests ?

Mile23’s picture

See #15. Contextual menus are javascript-based, so we have to use functional javascript tests. Here's a test from core: http://cgit.drupalcode.org/drupal/tree/core/modules/contextual/tests/src...

navneet0693’s picture

Status: Needs work » Needs review
FileSize
4.58 KB
21.63 KB

First attempt for javascript functional test.

Status: Needs review » Needs work

The last submitted patch, 23: examples-contextual-links-2102647-23.patch, failed testing. View results

navneet0693’s picture

Status: Needs work » Needs review
FileSize
3.75 KB
21.17 KB

Status: Needs review » Needs work

The last submitted patch, 25: examples-contextual-links-2102647-25.patch, failed testing. View results

Mile23’s picture

+++ b/contextual_links_example/tests/src/FunctionalJavascript/ContextualLinksExampleTest.php
@@ -0,0 +1,77 @@
+    $actionContextualLink = $assert->waitForElement('css', '.contextual-links-exampleexample-action');

Are you sure this class exists?

navneet0693’s picture

@Mile23 yes! I am attaching a screenshot of HTML output of JS test.

navneet0693’s picture

Status: Needs work » Needs review
FileSize
3.88 KB
21.46 KB

Everything needs to be perfect for tests to pass !

martin107’s picture

A minor adjustment

I have dropped t() in favour of $this->t().

navneet0693’s picture

+++ b/contextual_links_example/contextual_links_example.info.yml
@@ -0,0 +1,10 @@
+  - drupal:examples

Thank you @martin. Oops! My bad. Updating it to examples:examples

Mile23’s picture

Coming along...

  1. +++ b/contextual_links_example/contextual_links_example.module
    @@ -0,0 +1,30 @@
    +/**
    + * @file
    + * Shows how to use Drupal's contextual links functionality.
    + *
    + * @see https://www.drupal.org/docs/8/api/menu-api/providing-module-defined-contextual-links
    + */
    

    Needs @defgroup, and an explanation of what this module does and how it does it.

  2. +++ b/contextual_links_example/src/Controller/CLEController.php
    @@ -0,0 +1,55 @@
    +  public function clePage() {
    

    We should have some text on this page to explain what's being accomplished, generally how it works, and what to look at next for specifics.

  3. +++ b/contextual_links_example/src/Controller/CLEController.php
    @@ -0,0 +1,55 @@
    +    // For simplicity we hardcode an array of CLEEntity ids.
    +    $entity_ids = [1, 2, 3, 4, 5];
    

    I doubt these ids will always be the same. We should just load all the CLEEntity objects that are available.

  4. +++ b/contextual_links_example/src/Controller/CLEController.php
    @@ -0,0 +1,55 @@
    +      // See \Drupal\contextual\Element\ContextualLinks.
    

    Should be @see and not have a period at the end.

  5. +++ b/contextual_links_example/src/Entity/CLEEntity.php
    @@ -0,0 +1,48 @@
    + *     "storage" = "Drupal\contextual_links_example\Storage\CLEStorage"
    
    +++ b/contextual_links_example/src/Storage/CLEStorage.php
    @@ -0,0 +1,117 @@
    +    // TODO: Implement resetCache() method.
    ...
    +    // TODO: Implement loadMultiple() method.
    ...
    +    // TODO: Implement load() method.
    ...
    +    // TODO: Implement loadUnchanged() method.
    ...
    +    // TODO: Implement loadRevision() method.
    ...
    +    // TODO: Implement deleteRevision() method.
    ...
    +    // TODO: Implement loadByProperties() method.
    ...
    +    // TODO: Implement create() method.
    ...
    +    // TODO: Implement delete() method.
    ...
    +    // TODO: Implement save() method.
    ...
    +    // TODO: Implement getQuery() method.
    ...
    +    // TODO: Implement getAggregateQuery() method.
    ...
    +    // TODO: Implement getEntityTypeId() method.
    ...
    +    // TODO: Implement getEntityType() method.
    

    Is there not a generic entity base implementation we can use instead?

  6. +++ b/contextual_links_example/src/Form/ContextualLinksExampleNodeActionForm.php
    @@ -0,0 +1,129 @@
    + * @package Drupal\contextual_links_example\Form
    

    We don't use @package.

Mile23’s picture

Status: Needs review » Needs work
Lal_’s picture

Assigned: Unassigned » Lal_

working on it

Lal_’s picture

Still stuck with issue #5 and #3

Lal_’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 35: 2102647-32.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

navneet0693’s picture

+++ b/contextual_links_example/src/Controller/CLEController.php
@@ -0,0 +1,68 @@
+    $build[]=[

Not a correct way of doing this.

Use DescriptionTemplateTrait and $this->description to add in build array.

+++ a/examples.module
@@ -39,7 +39,6 @@
-    'contextual_links_example' => 'contextual_links_example.contextual_links_object_list',

Please do not remove this.

+++ b/contextual_links_example/tests/src/Functional/ContextualLinksExampleTest.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace Drupal\Tests\contextual_links_example\Functional;
+
+use Drupal\Tests\BrowserTestBase;
+
+/**
+ * Tests the behavior of the contextual links provided by the module.

Did you change something in tests ?

Lal_’s picture

Status: Needs work » Needs review
FileSize
23.82 KB

Status: Needs review » Needs work

The last submitted patch, 39: 2102647-39.patch, failed testing. View results

Lal_’s picture

Status: Needs work » Needs review
FileSize
23.87 KB
Lal_’s picture

Status: Needs review » Needs work

Need to fix few more issues

martin107’s picture

Status: Needs work » Needs review
FileSize
23.89 KB
3.53 KB

Just fixed a few coding standard nits while reading along.

Mile23’s picture

Assigned: Lal_ » Unassigned
navneet0693’s picture

Mile23’s picture

Title: Port contextual_links_example module to Drupal 8 » Add contextual_links_example module for Drupal 8
Issue summary: View changes

OK, so we have way too much stuff here just to implement an entity that we can't edit.

The storage class doesn't actually do anything.

This patch adds a test that demonstrates that we can't save our new entity.

So let's rescope a bit.

Let's add a contextual link implementation to the content entity example.

This means we can get rid of all the entity implementation details from this patch, and worry instead about adding contextual links.

Mile23’s picture

Hah. Forgot the patch.

Status: Needs review » Needs work

The last submitted patch, 47: 2102647_46.patch, failed testing. View results

valthebald’s picture

Version: 8.x-1.x-dev » 3.x-dev
Parent issue: » #3150762: [META] New examples for 9.4+

Moving to new examples meta