Browse Source

Step 3: implement skeletons for add/edit/delete forms and provide a schema.

Frederic G. MARAND 7 years ago
parent
commit
cc8f4493fe

+ 4 - 0
README.md

@@ -22,3 +22,7 @@ the following:
 1. Create the module and a `Blog` content entity in it.
 2. Create a `BlogType` config entity as the bundle entity for `Blog`, enabling
   its collection route.
+3. Implement skeletons for add/edit/delete forms and implement a `BlogType`
+  config entity schema to be able to test config import/export and the delete
+  form
+  

+ 6 - 0
config/install/dr8_zth.type.classic.yml

@@ -0,0 +1,6 @@
+langcode: en
+status: true
+dependencies: {  }
+name: Classic
+type: classic
+description: 'The classic textual blog'

+ 15 - 0
config/schema/dr8_zth.schema.yml

@@ -0,0 +1,15 @@
+# Schema for the configuration files of the DR8_ZTH module.
+
+blog.type.*:
+  type: config_entity
+  label: 'Blog type'
+  mapping:
+    description:
+      type: label
+      label: 'Description'
+    name:
+      type: label
+      label: 'Name'
+    type:
+      type: string
+      label: 'Machine name'

+ 11 - 0
dr8_zth.links.action.yml

@@ -0,0 +1,11 @@
+entity.blog_type.add_form:
+  route_name: entity.blog_type.add_form
+  title: 'Add blog type'
+  appears_on:
+    - entity.blog_type.collection
+
+#entity.taxonomy_term.add_form:
+#  route_name: entity.taxonomy_term.add_form
+#  title: 'Add term'
+#  appears_on:
+#    - entity.taxonomy_vocabulary.overview_form

+ 24 - 0
dr8_zth.routing.yml

@@ -6,3 +6,27 @@ entity.blog_type.collection:
   requirements:
     _permission: 'administer blog'
 
+entity.blog_type.add_form:
+  path: '/admin/structure/blog/manage/add'
+  defaults:
+    _entity_form: 'blog_type'
+    _title: 'Add blog type'
+  requirements:
+    _entity_create_access: 'blog_type'
+
+entity.blog_type.delete_form:
+  path: '/admin/structure/blog/manage/{blog_type}/delete'
+  defaults:
+    _entity_form: 'blog_type.delete'
+    _title: 'Delete blog type'
+  requirements:
+    _entity_access: 'blog_type.delete'
+
+entity.blog_type.edit_form:
+  path: '/admin/structure/blog/manage/{blog_type}'
+  defaults:
+    _entity_form: 'blog_type.default'
+    _title_callback: '\Drupal\dr8_zth\Controller\BlogController::typeTitle'
+  requirements:
+    _entity_access: 'blog_type.update'
+

+ 28 - 0
src/BlogTypeInterface.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Drupal\dr8_zth;
+
+
+use Drupal\Core\Config\Entity\ConfigEntityInterface;
+
+interface BlogTypeInterface extends ConfigEntityInterface {
+
+  /**
+   * Returns the blog type description.
+   *
+   * @return string
+   *   The vocabulary description.
+   */
+  public function getDescription();
+
+  /**
+   * Sets the blog type description.
+   *
+   * @param string $description
+   *   The description string.
+   *
+   * @return this
+   *   The blog_type instance.
+   */
+  public function setDescription($description);
+}

+ 57 - 0
src/Controller/BlogController.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace Drupal\dr8_zth\Controller;
+
+use Drupal\Component\Utility\Xss;
+use Drupal\Core\Controller\ControllerBase;
+use Drupal\dr8_zth\BlogTypeInterface;
+use Drupal\taxonomy\TermInterface;
+use Drupal\taxonomy\VocabularyInterface;
+
+/**
+ * Provides route responses for dr8_zth.module.
+ */
+class BlogController extends ControllerBase {
+
+  /**
+   * Returns a form to add a new term to a vocabulary.
+   *
+   * @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary
+   *   The vocabulary this term will be added to.
+   *
+   * @return array
+   *   The taxonomy term add form.
+   */
+  public function addForm(VocabularyInterface $taxonomy_vocabulary) {
+    $term = $this->entityManager()->getStorage('taxonomy_term')->create(array('vid' => $taxonomy_vocabulary->id()));
+    return $this->entityFormBuilder()->getForm($term);
+  }
+
+  /**
+   * Route title callback.
+   *
+   * @param \Drupal\dr8_zth\BlogTypeInterface $blog_type
+   *   The blog type instance.
+   *
+   * @return string
+   *   The blog type label as a render array.
+   *
+   */
+  public function typeTitle(BlogTypeInterface $blog_type) {
+    return ['#markup' => $blog_type->label(), '#allowed_tags' => Xss::getHtmlTagList()];
+  }
+
+  /**
+   * Route title callback.
+   *
+   * @param \Drupal\taxonomy\TermInterface $taxonomy_term
+   *   The taxonomy term.
+   *
+   * @return array
+   *   The term label as a render array.
+   */
+  public function termTitle(TermInterface $taxonomy_term) {
+    return ['#markup' => $taxonomy_term->getName(), '#allowed_tags' => Xss::getHtmlTagList()];
+  }
+
+}

+ 50 - 3
src/Entity/BlogType.php

@@ -2,8 +2,8 @@
 
 namespace Drupal\dr8_zth\Entity;
 
-
-use Drupal\Core\Config\Entity\ConfigEntityBase;
+use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
+use Drupal\dr8_zth\BlogTypeInterface;
 
 /**
  * Defines the taxonomy vocabulary entity.
@@ -12,6 +12,10 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
  *   id = "blog_type",
  *   label = @Translation("Blog type"),
  *   handlers = {
+ *     "form" = {
+ *       "default" = "Drupal\dr8_zth\Form\BlogTypeForm",
+ *       "delete" = "Drupal\dr8_zth\Form\BlogTypeDeleteForm"
+ *     },
  *     "list_builder" = "Drupal\Core\Config\Entity\ConfigEntityListBuilder",
  *   },
  *   admin_permission = "administer blog",
@@ -34,6 +38,49 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
  *   }
  * )
  */
-class BlogType extends ConfigEntityBase {
+class BlogType extends ConfigEntityBundleBase implements BlogTypeInterface {
+
+  /**
+   * The blog type description.
+   *
+   * @var string
+   */
+  protected $description;
+
+  /**
+   * The label entity_key.
+   *
+   * @var string
+   */
+  protected $name;
+
+  /**
+   * The id entity_key.
+   *
+   * @var string
+   */
+  protected $type;
+
+  /**
+   * {@inheritdoc}
+   */
+  public function id() {
+    return $this->type;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getDescription() {
+    return $this->description;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setDescription($description) {
+    $this->description = $description;
+    return $this;
+  }
 
 }

+ 42 - 0
src/Form/BlogTypeDeleteForm.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace Drupal\dr8_zth\Form;
+
+use Drupal\Core\Entity\EntityDeleteForm;
+
+/**
+ * Provides a deletion confirmation form for blog types.
+ */
+class BlogTypeDeleteForm extends EntityDeleteForm {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getFormId() {
+    return 'blog_type_confirm_delete';
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getQuestion() {
+    return $this->t('Are you sure you want to delete the %name blog type?', [
+      '%name' => $this->entity->label(),
+    ]);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getDescription() {
+    return $this->t('Deleting a blog type will delete all the blog items in it. This action cannot be undone.');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getDeletionMessage() {
+    return $this->t('Deleted %name blog type.', ['%name' => $this->entity->label()]);
+  }
+
+}

+ 15 - 0
src/Form/BlogTypeForm.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace Drupal\dr8_zth\Form;
+
+
+use Drupal\Core\Entity\BundleEntityFormBase;
+use Drupal\Core\Form\FormStateInterface;
+
+class BlogTypeForm extends BundleEntityFormBase {
+
+  public function form(array $form, FormStateInterface $form_state) {
+    $form = parent::form($form, $form_state);
+    return $this->protectBundleIdElement($form);
+  }
+}