From be2318526abf6a634af43f3d3d9f6f89a15c890d Mon Sep 17 00:00:00 2001
From: Bojan Zivanovic <bojanz@gmail.com>
Date: Tue, 1 Dec 2015 18:09:34 +0100
Subject: [PATCH] Addressed feedback.

- removed the classes to match core behavior.
- started using the link object.
- Added an interface for bundle entity descriptions.
- dawehner's fix for create_bundle_url in the preprocess function.
---
 entity.module                             | 19 ++++++-------
 src/Entity/EntityDescriptionInterface.php | 33 +++++++++++++++++++++++
 templates/entity-add-list.html.twig       |  3 +--
 3 files changed, 44 insertions(+), 11 deletions(-)
 create mode 100644 src/Entity/EntityDescriptionInterface.php

diff --git a/entity.module b/entity.module
index c206a41..3ee6637 100644
--- a/entity.module
+++ b/entity.module
@@ -5,6 +5,7 @@
  * Provides expanded entity APIs.
  */
 
+use Drupal\Core\Link;
 use Drupal\Core\Url;
 
 /**
@@ -37,19 +38,19 @@ function entity_theme() {
 function template_preprocess_entity_add_list(&$variables) {
   $bundle_type = \Drupal::entityTypeManager()->getDefinition($variables['bundle_type']);
   $variables += [
-    'class' => str_replace('_', '-', $bundle_type->getBundleOf()) . '-add-list',
-    'create_bundle_url' => Url::fromRoute('entity.' . $bundle_type->id() . '.add_form'),
+    'create_bundle_url' => Url::fromRoute('entity.' . $bundle_type->id() . '.add_form')->toString(),
     'bundle_type_label' => $bundle_type->getLowercaseLabel(),
   ];
 
   foreach ($variables['bundles'] as $bundle) {
-    $url = Url::fromRoute($variables['form_route_name'], [$bundle->getEntityTypeId() => $bundle->id()]);
-    $variables['bundles'][$bundle->id()] = [
-      'bundle' => $bundle->id(),
-      'add_link' => \Drupal::l($bundle->label(), $url),
-      'description' => [
-        '#markup' => $bundle->getDescription(),
-      ],
+    $bundle_id = $bundle->id();
+    $variables['bundles'][$bundle_id] = [
+      'add_link' => Link::createFromRoute($bundle->label(), $variables['form_route_name'], [$bundle_type->id() => $bundle_id]),
     ];
+    if ($bundle instanceof \Drupal\entity\Entity\EntityDescriptionInterface) {
+      $variables['bundles'][$bundle_id]['description'] = [
+        '#markup' => $bundle->getDescription(),
+      ];
+    }
   }
 }
diff --git a/src/Entity/EntityDescriptionInterface.php b/src/Entity/EntityDescriptionInterface.php
new file mode 100644
index 0000000..eb770e1
--- /dev/null
+++ b/src/Entity/EntityDescriptionInterface.php
@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\entity\Entity\EntityDescriptionInterface.
+ */
+
+namespace Drupal\entity\Entity;
+
+/**
+ * Defines the interface for entities that have a description.
+ */
+interface EntityDescriptionInterface {
+
+  /**
+   * Gets the entity description.
+   *
+   * @return string
+   *   The entity description.
+   */
+  public function getDescription();
+
+  /**
+   * Sets the entity description.
+   *
+   * @param string $description
+   *   The entity description.
+   *
+   * @return $this
+   */
+  public function setDescription($description);
+
+}
diff --git a/templates/entity-add-list.html.twig b/templates/entity-add-list.html.twig
index bd01070..55970ef 100644
--- a/templates/entity-add-list.html.twig
+++ b/templates/entity-add-list.html.twig
@@ -4,7 +4,6 @@
  * Default theme implementation to present a list of available bundles.
  *
  * Available variables:
- *   - class: The entity type specific class. E.g. 'node-add-list'.
  *   - create_bundle_url: The url to the bundle creation page.
  *   - bundle_type_label: The lowercase label of the bundle entity type.
  *   - bundles: A list of bundles, each with the following properties:
@@ -17,7 +16,7 @@
  */
 #}
 {% if bundles is not empty %}
-  <dl class="entity-add-list {{ class }}">
+  <dl>
     {% for bundle in bundles %}
       <dt>{{ bundle.add_link }}</dt>
       <dd>{{ bundle.description }}</dd>
-- 
GitLab