8c8f5fac69967aeca9c08ccdd9164eebb28dc65d
[yaffs-website] / web / core / modules / language / src / Form / LanguageFormBase.php
1 <?php
2
3 namespace Drupal\language\Form;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Entity\EntityForm;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\language\ConfigurableLanguageManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Base form for language add and edit forms.
14  */
15 abstract class LanguageFormBase extends EntityForm {
16
17   /**
18    * The configurable language manager.
19    *
20    * @var \Drupal\language\ConfigurableLanguageManagerInterface
21    */
22   protected $languageManager;
23
24   /**
25    * Constructs a ContentEntityForm object.
26    *
27    * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
28    *   The configurable language manager.
29    */
30   public function __construct(ConfigurableLanguageManagerInterface $language_manager) {
31     $this->languageManager = $language_manager;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static(
39       $container->get('language_manager')
40     );
41   }
42
43   /**
44    * Common elements of the language addition and editing form.
45    */
46   public function commonForm(array &$form) {
47     /* @var $language \Drupal\language\ConfigurableLanguageInterface */
48     $language = $this->entity;
49     if ($language->getId()) {
50       $form['langcode_view'] = [
51         '#type' => 'item',
52         '#title' => $this->t('Language code'),
53         '#markup' => $language->id()
54       ];
55       $form['langcode'] = [
56         '#type' => 'value',
57         '#value' => $language->id()
58       ];
59     }
60     else {
61       $form['langcode'] = [
62         '#type' => 'textfield',
63         '#title' => $this->t('Language code'),
64         '#maxlength' => 12,
65         '#required' => TRUE,
66         '#default_value' => '',
67         '#disabled' => FALSE,
68         '#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/']),
69       ];
70     }
71     $form['label'] = [
72       '#type' => 'textfield',
73       '#title' => $this->t('Language name'),
74       '#maxlength' => 64,
75       '#default_value' => $language->label(),
76       '#required' => TRUE,
77     ];
78     $form['direction'] = [
79       '#type' => 'radios',
80       '#title' => $this->t('Direction'),
81       '#required' => TRUE,
82       '#description' => $this->t('Direction that text in this language is presented.'),
83       '#default_value' => $language->getDirection(),
84       '#options' => [
85         LanguageInterface::DIRECTION_LTR => $this->t('Left to right'),
86         LanguageInterface::DIRECTION_RTL => $this->t('Right to left'),
87       ],
88     ];
89
90     return $form;
91   }
92
93   /**
94    * Validates the language editing element.
95    */
96   public function validateCommon(array $form, FormStateInterface $form_state) {
97     // Ensure sane field values for langcode and name.
98     if (!isset($form['langcode_view']) && !preg_match('@^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$@', $form_state->getValue('langcode'))) {
99       $form_state->setErrorByName('langcode', $this->t('%field must be a valid language tag as <a href=":url">defined by the W3C</a>.', [
100         '%field' => $form['langcode']['#title'],
101         ':url' => 'http://www.w3.org/International/articles/language-tags/',
102       ]));
103     }
104     if ($form_state->getValue('label') != Html::escape($form_state->getValue('label'))) {
105       $form_state->setErrorByName('label', $this->t('%field cannot contain any markup.', ['%field' => $form['label']['#title']]));
106     }
107   }
108
109 }