|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
}
|