View source
<?php
namespace Drupal\languagefield\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
class CustomLanguageForm extends EntityForm {
public function form(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
if ($entity
->isNew()) {
$form['langcode'] = [
'#type' => 'textfield',
'#title' => $this
->t('Language code'),
'#default_value' => '',
'#size' => 10,
'#required' => TRUE,
'#maxlength' => 10,
'#description' => $this
->t('Use language codes as <a href=":w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', [
':w3ctags' => 'http://www.w3.org/International/articles/language-tags/',
]),
];
}
else {
$form['langcode_view'] = [
'#type' => 'item',
'#title' => $this
->t('Language code'),
'#markup' => $entity
->id(),
];
$form['langcode'] = [
'#type' => 'value',
'#value' => $entity
->id(),
];
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Language name'),
'#default_value' => $entity
->label(),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
];
$form['native_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Native Name'),
'#default_value' => $entity
->getNativeName(),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
];
return parent::form($form, $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
$entity = $this->entity;
$action = $status == SAVED_UPDATED ? 'updated' : 'added';
$this
->messenger()
->addStatus($this
->t('The language %label has been %action.', [
'%label' => $entity
->label(),
'%action' => $action,
]));
$this
->logger('languagefield')
->notice('The language %label has been %action.', [
'%label' => $entity
->label(),
'%action' => $action,
]);
$form_state
->setRedirect('languagefield.custom_language.collection');
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->entity
->isNew() ? $this
->t('Add custom language') : $this
->t('Save language');
return $actions;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!isset($form['langcode_view']) && !preg_match('@^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$@', $form_state
->getValue('langcode'))) {
$form_state
->setErrorByName('langcode', $this
->t('%field must be a valid language tag as <a href=":url">defined by the W3C</a>.', [
'%field' => $form['langcode']['#title'],
':url' => 'http://www.w3.org/International/articles/language-tags/',
]));
}
foreach ([
'label',
'native_name',
] as $field) {
if ($form_state
->getValue($field) != Html::escape($form_state
->getValue($field))) {
$form_state
->setErrorByName($field, $this
->t('%field cannot contain any markup.', [
'%field' => $form[$field]['#title'],
]));
}
}
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$langcode = trim($form_state
->getValue('langcode'));
$label = trim($form_state
->getValue('label'));
$direction = trim($form_state
->getValue('direction'));
$native = trim($form_state
->getValue('native_name'));
$entity
->set('id', $langcode);
$entity
->set('label', $label);
$entity
->set('native_name', $native);
$entity
->set('direction', $direction);
}
}