Browse Source

Step 5: complete the BlogType CRUD.

Frederic G. MARAND 8 years ago
parent
commit
a63108ab9e

+ 1 - 0
README.md

@@ -27,3 +27,4 @@ the following:
   form
 4. Complete the implementation of the `BlogType` collection route to show type,
   name, description, and operations.
+5. Complete the `BlogType` CRUD by implementing the `BlogTypeForm`.

+ 4 - 1
src/BlogTypeInterface.php

@@ -2,9 +2,11 @@
 
 namespace Drupal\dr8_zth;
 
-
 use Drupal\Core\Config\Entity\ConfigEntityInterface;
 
+/**
+ * The specific operations of the BlogType entity type.
+ */
 interface BlogTypeInterface extends ConfigEntityInterface {
 
   /**
@@ -25,4 +27,5 @@ interface BlogTypeInterface extends ConfigEntityInterface {
    *   The blog_type instance.
    */
   public function setDescription($description);
+
 }

+ 2 - 0
src/BlogTypeListBuilder.php

@@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityInterface;
  * Class BlogTypeListBuilder is the list builder for BlogType entities.
  */
 class BlogTypeListBuilder extends ConfigEntityListBuilder {
+
   /**
    * {@inheritdoc}
    */
@@ -37,4 +38,5 @@ class BlogTypeListBuilder extends ConfigEntityListBuilder {
     $ret = array_merge($row, parent::buildRow($entity));
     return $ret;
   }
+
 }

+ 0 - 1
src/Controller/BlogController.php

@@ -35,7 +35,6 @@ class BlogController extends ControllerBase {
    *
    * @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()];

+ 0 - 1
src/Entity/Blog.php

@@ -2,7 +2,6 @@
 
 namespace Drupal\dr8_zth\Entity;
 
-
 use Drupal\Core\Entity\ContentEntityBase;
 
 /**

+ 117 - 1
src/Form/BlogTypeForm.php

@@ -2,14 +2,130 @@
 
 namespace Drupal\dr8_zth\Form;
 
-
+use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
 use Drupal\Core\Entity\BundleEntityFormBase;
+use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
+/**
+ * Class BlogTypeForm is the default add/edit form for BlogType entities.
+ */
 class BlogTypeForm extends BundleEntityFormBase {
 
+  /**
+   * The blog type storage.
+   *
+   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
+   */
+  protected $storage;
+
+  /**
+   * BlogTypeForm constructor.
+   *
+   * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage
+   *   The blog_type storage.
+   */
+  public function __construct(ConfigEntityStorageInterface $storage) {
+    $this->storage = $storage;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    $storage = $container->get('entity_type.manager')->getStorage('blog_type');
+    return new static($storage);
+  }
+
+  /**
+   * Determines if the blog type already exists.
+   *
+   * @param string $type
+   *   The blog type ID.
+   *
+   * @return bool
+   *   TRUE if the blog type exists, FALSE otherwise.
+   */
+  public function exists($type) {
+    $action = $this->storage->load($type);
+    return !empty($action);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public function form(array $form, FormStateInterface $form_state) {
+    $type = $this->entity;
+
+    $form['#title'] = $type->isNew()
+      ? $this->t('Add blog type')
+      : $this->t('Edit blog type');
+
+    $form['name'] = array(
+      '#type' => 'textfield',
+      '#title' => $this->t('Name'),
+      '#default_value' => $type->label(),
+      '#maxlength' => 255,
+      '#required' => TRUE,
+    );
+    $form['type'] = array(
+      '#type' => 'machine_name',
+      '#default_value' => $type->id(),
+      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
+      '#machine_name' => array(
+        'exists' => array($this, 'exists'),
+        'source' => array('name'),
+      ),
+    );
+    $form['description'] = array(
+      '#type' => 'textfield',
+      '#title' => $this->t('Description'),
+      '#default_value' => $type->getDescription(),
+    );
+
     $form = parent::form($form, $form_state);
     return $this->protectBundleIdElement($form);
   }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function save(array $form, FormStateInterface $form_state) {
+    $channel = 'dr8_zth';
+    $type = $this->entity;
+
+    // Prevent leading and trailing spaces in names.
+    $type->set('name', trim($type->label()));
+
+    $status = $type->save();
+    $edit_link = $this->entity
+      ->toLink($this->t('Edit'), 'edit-form')
+      ->toString();
+    $event_args = [
+      '%name' => $type->label(),
+      'link' => $edit_link,
+    ];
+    $collection = $type->toUrl('collection');
+
+    switch ($status) {
+      case SAVED_NEW:
+        drupal_set_message($this->t('Created new %name blog type.', $event_args));
+        $this->logger($channel)
+          ->notice('Created new %name blog type.', $event_args);
+        break;
+
+      case SAVED_UPDATED:
+        drupal_set_message($this->t('Updated %name blog type.', $event_args));
+        $this->logger($channel)
+          ->notice('Updated %name blog type.', $event_args);
+        break;
+    }
+
+    $form_state->setRedirectUrl($collection);
+    $id = $type->id();
+    $form_state->setValue('type', $id);
+    $form_state->set('type', $id);
+  }
+
 }