View source
<?php
namespace Drupal\entity_share_server\Form;
use Drupal\Core\Form\FormStateInterface;
class SortEditForm extends SortBaseForm {
protected $sortId;
public function form(array $form, FormStateInterface $form_state) {
if (!$this
->sortIdExists()) {
drupal_set_message($this
->t('There is no sort with the ID @id in this channel', [
'@id' => $this
->getsortId(),
]), 'error');
return [];
}
$form = parent::form($form, $form_state);
$channel = $this->entity;
$channel_sorts = $channel
->get('channel_sorts');
$sort_id = $this
->getsortId();
$form['path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path'),
'#description' => $this
->t('Enter the machine name of the field / property you want to sort on. You can reference field / property of a referenced entity. Example: uid.name for the name of the author.'),
'#required' => TRUE,
'#default_value' => $channel_sorts[$sort_id]['path'],
];
$form['sort_id'] = [
'#type' => 'machine_name',
'#title' => $this
->t('ID'),
'#default_value' => $sort_id,
'#machine_name' => [
'source' => [
'path',
],
'exists' => [
$this,
'sortExists',
],
],
'#disabled' => TRUE,
];
$form['direction'] = [
'#type' => 'select',
'#title' => $this
->t('Direction'),
'#options' => $this
->getDirectionOptions(),
'#default_value' => $channel_sorts[$sort_id]['direction'],
'#required' => TRUE,
];
$form['weight'] = [
'#type' => 'number',
'#title' => $this
->t('Weight'),
'#default_value' => $channel_sorts[$sort_id]['weight'],
'#required' => TRUE,
];
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$channel = $this->entity;
$channel_sorts = $channel
->get('channel_sorts');
$channel_sorts[$form_state
->getValue('sort_id')] = [
'path' => $form_state
->getValue('path'),
'direction' => $form_state
->getValue('direction'),
'weight' => $form_state
->getValue('weight'),
];
$channel
->set('channel_sorts', $channel_sorts);
$channel
->save();
$form_state
->setRedirectUrl($channel
->toUrl('edit-form'));
}
protected function actions(array $form, FormStateInterface $form_state) {
if (!$this
->sortIdExists()) {
return [];
}
return parent::actions($form, $form_state);
}
}