CrossdomainFormController.php in Crossdomain 8
File
lib/Drupal/crossdomain/Controller/CrossdomainFormController.php
View source
<?php
namespace Drupal\crossdomain\Controller;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFormController;
use Drupal\Core\Entity\Query\QueryFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CrossdomainFormController extends EntityFormController {
protected $queryFactory;
public function __construct(QueryFactory $query_factory) {
$this->queryFactory = $query_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'));
}
public function form(array $form, array &$form_state) {
$form = parent::form($form, $form_state);
$domain = $this->entity;
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Domain'),
'#maxlength' => 255,
'#default_value' => $domain
->label(),
'#description' => $this
->t('Domain to add to the xml file.'),
'#required' => TRUE,
);
$form['id'] = array(
'#type' => 'machine_name',
'#default_value' => $domain
->id(),
'#machine_name' => array(
'exists' => array(
$this,
'exists',
),
),
'#disabled' => !$domain
->isNew(),
);
return $form;
}
public function save(array $form, array &$form_state) {
$domain = $this->entity;
$status = $domain
->save();
if ($status) {
drupal_set_message($this
->t('Saved %domain.', array(
'%domain' => $domain
->label(),
)));
}
else {
drupal_set_message($this
->t('The %domain was not saved.', array(
'%domain' => $domain
->label(),
)));
}
$form_state['redirect'] = 'admin/config/media/crossdomain';
}
public function delete(array $form, array &$form_state) {
$destination = array();
$request = $this
->getRequest();
if ($request->query
->has('destination')) {
$destination = drupal_get_destination();
$request->query
->remove('destination');
}
$entity = $this
->getEntity($form_state);
$form_state['redirect'] = array(
'admin/config/media/crossdomain/' . $entity
->id() . '/delete',
array(
'query' => $destination,
),
);
}
public function setEntity(EntityInterface $entity) {
$this->entity = $entity;
return $this;
}
public function exists($entity_id) {
return (bool) $this->queryFactory
->get('crossdomain')
->condition('id', $entity_id)
->execute();
}
}