BlazyEntityFormBase.php in Blazy 8.2
File
blazy_ui/src/Form/BlazyEntityFormBase.php
View source
<?php
namespace Drupal\blazy_ui\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
abstract class BlazyEntityFormBase extends EntityForm {
protected static $niceName = 'Slick';
protected static $machineName = 'slick';
protected $admin;
protected $manager;
protected $formElements;
public function admin() {
return $this->admin;
}
public function manager() {
return $this->manager;
}
public function form(array $form, FormStateInterface $form_state) {
$admin_css = $this->manager
->configLoad('admin_css', 'blazy.settings');
$form['#attributes']['class'][] = 'form--blazy form--slick form--optionset has-tooltip';
if ($this->operation == 'duplicate') {
$form['#title'] = $this
->t('<em>Duplicate %name optionset</em>: @label', [
'%name' => static::$niceName,
'@label' => $this->entity
->label(),
]);
$this->entity = $this->entity
->createDuplicate();
}
if ($this->operation == 'edit') {
$form['#title'] = $this
->t('<em>Edit %name optionset</em>: @label', [
'%name' => static::$niceName,
'@label' => $this->entity
->label(),
]);
}
if ($admin_css && $this->manager
->getModuleHandler()
->moduleExists('slick_ui')) {
$form['#attached']['library'][] = 'slick_ui/slick.admin.vtabs';
}
return parent::form($form, $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
$optionset = $this->entity;
$optionset
->set('label', trim($optionset
->label()));
$optionset
->set('id', $optionset
->id());
$status = $optionset
->save();
$label = $optionset
->label();
$edit_link = $optionset
->toLink($this
->t('Edit'), 'edit-form')
->toString();
$config_prefix = $optionset
->getEntityType()
->getConfigPrefix();
$message = [
'@config_prefix' => $config_prefix,
'%label' => $label,
];
$notice = [
'@config_prefix' => $config_prefix,
'%label' => $label,
'link' => $edit_link,
];
if ($status == SAVED_UPDATED) {
$this
->messenger()
->addMessage($this
->t('@config_prefix %label has been updated.', $message));
$this
->logger(static::$machineName)
->notice('@config_prefix %label has been updated.', $notice);
}
else {
$this
->messenger()
->addMessage($this
->t('@config_prefix %label has been added.', $message));
$this
->logger(static::$machineName)
->notice('@config_prefix %label has been added.', $notice);
}
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
}
}