|
@@ -2,31 +2,30 @@
|
|
|
|
|
|
namespace Drupal\dr8_zth\Form;
|
|
|
|
|
|
-use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
|
|
|
use Drupal\Core\Entity\ContentEntityForm;
|
|
|
-use Drupal\Core\Entity\EntityTypeInterface;
|
|
|
+use Drupal\Core\Entity\ContentEntityStorageInterface;
|
|
|
use Drupal\Core\Form\FormStateInterface;
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
|
|
/**
|
|
|
- * Class BlogTypeForm is the default add/edit form for BlogType entities.
|
|
|
+ * Class BlogForm is the default add/edit form for Blog entities.
|
|
|
*/
|
|
|
class BlogForm extends ContentEntityForm {
|
|
|
|
|
|
/**
|
|
|
* The blog type storage.
|
|
|
*
|
|
|
- * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
|
|
|
+ * @var \Drupal\Core\Entity\ConfigEntityStorageInterface
|
|
|
*/
|
|
|
protected $storage;
|
|
|
|
|
|
/**
|
|
|
* BlogTypeForm constructor.
|
|
|
*
|
|
|
- * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage
|
|
|
- * The blog_type storage.
|
|
|
+ * @param \Drupal\Core\Entity\ContentEntityStorageInterface $storage
|
|
|
+ * The blog storage.
|
|
|
*/
|
|
|
- public function __construct(ConfigEntityStorageInterface $storage) {
|
|
|
+ public function __construct(ContentEntityStorageInterface $storage) {
|
|
|
$this->storage = $storage;
|
|
|
}
|
|
|
|
|
@@ -34,7 +33,7 @@ class BlogForm extends ContentEntityForm {
|
|
|
* {@inheritdoc}
|
|
|
*/
|
|
|
public static function create(ContainerInterface $container) {
|
|
|
- $storage = $container->get('entity_type.manager')->getStorage('blog_type');
|
|
|
+ $storage = $container->get('entity_type.manager')->getStorage('blog');
|
|
|
return new static($storage);
|
|
|
}
|
|
|
|
|
@@ -56,36 +55,20 @@ class BlogForm extends ContentEntityForm {
|
|
|
* {@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);
|
|
|
+ $blog = $this->entity;
|
|
|
+
|
|
|
+ $form['type'] = [
|
|
|
+ '#type' => 'value',
|
|
|
+ '#value' => $blog->bundle(),
|
|
|
+ ];
|
|
|
+
|
|
|
+ $form['blog_id'] = [
|
|
|
+ '#type' => 'value',
|
|
|
+ '#value' => $blog->id(),
|
|
|
+ ];
|
|
|
+
|
|
|
+ $form = parent::form($form, $form_state, $blog);
|
|
|
+ return $form;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -93,39 +76,38 @@ class BlogForm extends ContentEntityForm {
|
|
|
*/
|
|
|
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()));
|
|
|
+ $blog = $this->entity;
|
|
|
+ $result = $blog->save();
|
|
|
|
|
|
- $status = $type->save();
|
|
|
- $edit_link = $this->entity
|
|
|
+ $editLink = $blog
|
|
|
->toLink($this->t('Edit'), 'edit-form')
|
|
|
->toString();
|
|
|
+ $viewLink = $blog
|
|
|
+ ->toLink($blog->label(), 'canonical')
|
|
|
+ ->toString();
|
|
|
$event_args = [
|
|
|
- '%name' => $type->label(),
|
|
|
- 'link' => $edit_link,
|
|
|
+ '%blog' => $viewLink,
|
|
|
+ 'link' => $editLink,
|
|
|
];
|
|
|
- $collection = $type->toUrl('collection');
|
|
|
|
|
|
- switch ($status) {
|
|
|
+ switch ($result) {
|
|
|
case SAVED_NEW:
|
|
|
- drupal_set_message($this->t('Created new %name blog type.', $event_args));
|
|
|
+ drupal_set_message($this->t('Created new %blog blog.', $event_args));
|
|
|
$this->logger($channel)
|
|
|
- ->notice('Created new %name blog type.', $event_args);
|
|
|
+ ->notice('Created new %blog blog.', $event_args);
|
|
|
break;
|
|
|
|
|
|
case SAVED_UPDATED:
|
|
|
- drupal_set_message($this->t('Updated %name blog type.', $event_args));
|
|
|
+ drupal_set_message($this->t('Updated %blog blog.', $event_args));
|
|
|
$this->logger($channel)
|
|
|
- ->notice('Updated %name blog type.', $event_args);
|
|
|
+ ->notice('Updated %name blog.', $event_args);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- $form_state->setRedirectUrl($collection);
|
|
|
- $id = $type->id();
|
|
|
- $form_state->setValue('type', $id);
|
|
|
- $form_state->set('type', $id);
|
|
|
+ $form_state->setRedirectUrl($blog->toUrl('canonical'));
|
|
|
+ $id = $blog->id();
|
|
|
+ $form_state->setValue('blog_id', $id);
|
|
|
+ $form_state->set('blog_id', $id);
|
|
|
}
|
|
|
|
|
|
}
|