View source
<?php
namespace Drupal\splashify\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class SplashifyGroupEntityForm extends ContentEntityForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$form = parent::buildForm($form, $form_state);
$form['field_list_pages']['widget'][0]['value']['#states'] = array(
'visible' => array(
':input[name="field_where"]' => array(
'value' => 'list',
),
),
'required' => array(
':input[name="field_where"]' => array(
'value' => 'list',
),
),
);
$form['field_roles']['#states'] = array(
'visible' => array(
':input[name="field_restrict[value]"]' => array(
'checked' => TRUE,
),
),
);
$form['field_splash_mode']['#states'] = array(
'invisible' => array(
':input[name="field_mode"]' => array(
'value' => 'full_screen',
),
),
);
$form['field_size']['widget'][0]['value']['#states'] = array(
'visible' => array(
':input[name="field_mode"]' => array(
array(
'value' => 'window',
),
array(
'value' => 'lightbox',
),
),
),
'required' => array(
':input[name="field_mode"]' => array(
array(
'value' => 'window',
),
array(
'value' => 'lightbox',
),
),
),
);
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this
->t('Created the %label Splashify group entity.', [
'%label' => $entity
->label(),
]));
break;
default:
drupal_set_message($this
->t('Saved the %label Splashify group entity.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.splashify_group_entity.canonical', [
'splashify_group_entity' => $entity
->id(),
]);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$values = $form_state
->getValues();
if ($values['field_opposite']['value']) {
if ($values['field_where'][0]['value'] == 'all') {
$form_state
->setError($form['field_where']['widget'], $this
->t('The "All" option cannot be selected when checking the "Opposite" option.'));
}
}
$paths = $values['field_list_pages'][0]['value'];
$what_paths = preg_split('/[\\n\\r]+/', $paths);
$errors = array();
foreach ($what_paths as $path) {
if ($is_valid = \Drupal::service('path.validator')
->isValid($path)) {
continue;
}
if (substr($path, 0, 7) == 'http://') {
continue;
}
$errors[] .= t('The path "@path" is not valid.', array(
'@path' => $path,
));
}
if (count($errors) > 0) {
$form_state
->setError($form['field_list_pages']['widget'], implode('<br />', $errors));
}
if (empty($paths) && $values['field_where'][0]['value'] == 'list') {
$form_state
->setError($form['field_list_pages']['widget'], $this
->t("Field 'List Pages' can't be empty"));
}
}
}