Commit 0f56bfa3 authored by alexpott's avatar alexpott

Issue #2156923 by amateescu, jayeshanandani, miraj9093, Xano, InternetDevels:...

Issue #2156923 by amateescu, jayeshanandani, miraj9093, Xano, InternetDevels: Various code cleanups for the new shortcut entity.
parent 48025f92
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\shortcut\Plugin\Core\Entity\Shortcut.
* Contains \Drupal\shortcut\Entity\Shortcut.
*/
namespace Drupal\shortcut\Entity;
......@@ -11,7 +11,6 @@
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Routing\UrlMatcher;
use Drupal\Core\Url;
use Drupal\shortcut\ShortcutInterface;
......@@ -21,7 +20,6 @@
* @ContentEntityType(
* id = "shortcut",
* label = @Translation("Shortcut link"),
* module = "shortcut",
* controllers = {
* "access" = "Drupal\shortcut\ShortcutAccessController",
* "form" = {
......
......@@ -9,11 +9,10 @@
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\shortcut\ShortcutInterface;
use Drupal\shortcut\ShortcutSetInterface;
/**
* Defines the Shortcut configuration entity.
* Defines the Shortcut set configuration entity.
*
* @ConfigEntityType(
* id = "shortcut_set",
......
......@@ -17,30 +17,11 @@
class SetCustomize extends EntityFormController {
/**
* The shortcut storage controller.
* The entity being used by this form.
*
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
* @var \Drupal\shortcut\ShortcutSetInterface
*/
protected $storageController;
/**
* Constructs a SetCustomize object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entity_manager) {
$this->storageController = $entity_manager->getStorageController('shortcut');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')
);
}
protected $entity;
/**
* {@inheritdoc}
......@@ -66,8 +47,7 @@ public function form(array $form, array &$form_state) {
),
);
$shortcuts = $this->storageController->loadByProperties(array('shortcut_set' => $this->entity->id()));
foreach ($shortcuts as $shortcut) {
foreach ($this->entity->getShortcuts() as $shortcut) {
$id = $shortcut->id();
$form['shortcuts']['links'][$id]['#attributes']['class'][] = 'draggable';
$form['shortcuts']['links'][$id]['name']['#markup'] = l($shortcut->getTitle(), $shortcut->path->value);
......@@ -119,8 +99,7 @@ protected function actions(array $form, array &$form_state) {
* {@inheritdoc}
*/
public function save(array $form, array &$form_state) {
$shortcuts = $this->storageController->loadByProperties(array('shortcut_set' => $this->entity->id()));
foreach ($shortcuts as $shortcut) {
foreach ($this->entity->getShortcuts() as $shortcut) {
$shortcut->setWeight($form_state['values']['shortcuts']['links'][$shortcut->id()]['weight']);
$shortcut->save();
}
......
......@@ -8,13 +8,7 @@
namespace Drupal\shortcut;
use Drupal\Core\Entity\ContentEntityFormController;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for the shortcut entity forms.
......@@ -22,68 +16,22 @@
class ShortcutFormController extends ContentEntityFormController {
/**
* The path alias manager.
* The entity being used by this form.
*
* @var \Drupal\Core\Path\AliasManagerInterface
* @var \Drupal\shortcut\ShortcutInterface
*/
protected $aliasManager;
/**
* The URL generator.
*
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* Constructs a new action form.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
* The path alias manager.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The URL generator.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder.
*/
public function __construct(EntityManagerInterface $entity_manager, AliasManagerInterface $alias_manager, UrlGeneratorInterface $url_generator, FormBuilderInterface $form_builder) {
$this->entityManager = $entity_manager;
$this->aliasManager = $alias_manager;
$this->urlGenerator = $url_generator;
$this->formBuilder = $form_builder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('path.alias_manager'),
$container->get('url_generator'),
$container->get('form_builder')
);
}
protected $entity;
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$form = parent::form($form, $form_state);
$entity = $this->entity;
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $entity->getTitle(),
'#default_value' => $this->entity->getTitle(),
'#size' => 40,
'#maxlength' => 255,
'#required' => TRUE,
......@@ -95,28 +43,28 @@ public function form(array $form, array &$form_state) {
'#title' => t('Path'),
'#size' => 40,
'#maxlength' => 255,
'#field_prefix' => $this->urlGenerator->generateFromRoute('<front>', array(), array('absolute' => TRUE)),
'#default_value' => $entity->path->value,
'#field_prefix' => $this->url('<front>', array(), array('absolute' => TRUE)),
'#default_value' => $this->entity->path->value,
);
$form['langcode'] = array(
'#title' => t('Language'),
'#type' => 'language_select',
'#default_value' => $entity->getUntranslated()->language()->id,
'#default_value' => $this->entity->getUntranslated()->language()->id,
'#languages' => Language::STATE_ALL,
);
$form['shortcut_set'] = array(
'#type' => 'value',
'#value' => $entity->bundle(),
'#value' => $this->entity->bundle(),
);
$form['route_name'] = array(
'#type' => 'value',
'#value' => $entity->getRouteName(),
'#value' => $this->entity->getRouteName(),
);
$form['route_parameters'] = array(
'#type' => 'value',
'#value' => $entity->getRouteParams(),
'#value' => $this->entity->getRouteParams(),
);
return $form;
......@@ -140,7 +88,7 @@ public function buildEntity(array $form, array &$form_state) {
*/
public function validate(array $form, array &$form_state) {
if (!shortcut_valid_link($form_state['values']['path'])) {
$this->formBuilder->setErrorByName('path', $form_state, $this->t('The shortcut must correspond to a valid path on the site.'));
$this->setFormError('path', $form_state, $this->t('The shortcut must correspond to a valid path on the site.'));
}
parent::validate($form, $form_state);
......
......@@ -44,7 +44,7 @@ public function getWeight();
/**
* Sets the weight among shortcuts with the same depth.
*
*·@param int $weight
* @param int $weight
* The shortcut weight.
*
* @return \Drupal\shortcut\ShortcutInterface
......
......@@ -10,7 +10,7 @@
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface defining a shortcut entity.
* Provides an interface defining a shortcut set entity.
*/
interface ShortcutSetInterface extends ConfigEntityInterface {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment