View source
<?php
namespace Drupal\royalslider\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RoyalSliderOptionSetForm extends EntityForm {
public function __construct(QueryFactory $entity_query) {
$this->entityQuery = $entity_query;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$optionset = $this->entity;
if ($this->operation == 'edit') {
$form['#title'] = $this
->t('Edit optionset: @name', array(
'@name' => $optionset->name,
));
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#maxlength' => 255,
'#default_value' => $optionset->name,
'#description' => $this
->t("Optionset name."),
'#required' => TRUE,
);
$form['id'] = array(
'#type' => 'machine_name',
'#default_value' => $optionset
->id(),
'#machine_name' => array(
'exists' => array(
$this,
'exist',
),
),
'#disabled' => !$optionset
->isNew(),
);
$form['auto_scale_slider'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('autoScaleSlider'),
'#default_value' => $optionset->auto_scale_slider,
'#description' => $this
->t("Automatically updates slider height based on base width."),
);
$form['auto_scale_slider_width'] = array(
'#type' => 'textfield',
'#title' => $this
->t('autoScaleSliderWidth'),
'#default_value' => $optionset->auto_scale_slider_width,
'#description' => $this
->t("Base slider width. Slider will autocalculate the ratio based on these values."),
);
$form['auto_scale_slider_height'] = array(
'#type' => 'textfield',
'#title' => $this
->t('autoScaleSliderHeight'),
'#default_value' => $optionset->auto_scale_slider_height,
'#description' => $this
->t("Base slider height."),
);
$form['loop'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('loop'),
'#default_value' => $optionset->loop,
'#description' => $this
->t("The slideshow loops."),
);
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$optionset = $this->entity;
$status = $optionset
->save();
if ($status) {
drupal_set_message($this
->t('Saved the %label optionset.', array(
'%label' => $optionset
->label(),
)));
}
else {
drupal_set_message($this
->t('The %label optionset was not saved.', array(
'%label' => $optionset
->label(),
)));
}
$form_state
->setRedirect('entity.royalslider_optionset.collection');
}
public function exist($id) {
$entity = $this->entityQuery
->get('royalslider_optionset')
->condition('id', $id)
->execute();
return (bool) $entity;
}
}