Commit 0aa6f4e5 authored by webchick's avatar webchick

Issue #2003592 by andypost, mhagedon, webflo: Convert...

Issue #2003592 by andypost, mhagedon, webflo: Convert language_admin_add_form() and language_admin_edit_form() to a Controllers.
parent 1b4f1046
......@@ -5,213 +5,7 @@
* Administration functions for language.module.
*/
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* User interface for the language addition screen.
*/
function language_admin_add_form($form, &$form_state) {
$predefined_languages = language_admin_predefined_list();
$predefined_languages['custom'] = t('Custom language...');
$predefined_default = !empty($form_state['values']['predefined_langcode']) ? $form_state['values']['predefined_langcode'] : key($predefined_languages);
$form['predefined_langcode'] = array(
'#type' => 'select',
'#title' => t('Language name'),
'#default_value' => $predefined_default,
'#options' => $predefined_languages,
);
$form['predefined_submit'] = array(
'#type' => 'submit',
'#value' => t('Add language'),
'#limit_validation_errors' => array(array('predefined_langcode'), array('predefined_submit')),
'#states' => array(
'invisible' => array(
'select#edit-predefined-langcode' => array('value' => 'custom'),
),
),
'#validate' => array('language_admin_add_predefined_form_validate'),
'#submit' => array('language_admin_add_form_submit'),
);
$form['custom_language'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
'select#edit-predefined-langcode' => array('value' => 'custom'),
),
),
);
_language_admin_common_controls($form['custom_language']);
$form['custom_language']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add custom language'),
'#validate' => array('language_admin_add_custom_form_validate'),
'#submit' => array('language_admin_add_form_submit'),
);
return $form;
}
/**
* Editing screen for a particular language.
*
* @param $langcode
* Language code of the language to edit.
*/
function language_admin_edit_form($form, &$form_state, $language) {
_language_admin_common_controls($form, $language);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save language')
);
$form['#submit'][] = 'language_admin_edit_form_submit';
$form['#validate'][] = 'language_admin_edit_form_validate';
return $form;
}
/**
* Common elements of the language addition and editing form.
*
* @param $form
* A parent form item (or empty array) to add items below.
* @param $language
* Language object to edit.
*/
function _language_admin_common_controls(&$form, $language = NULL) {
if (!is_object($language)) {
$language = new Language(array('id' => NULL, 'name' => NULL,));
}
if (isset($language->id)) {
$form['langcode_view'] = array(
'#type' => 'item',
'#title' => t('Language code'),
'#markup' => $language->id
);
$form['langcode'] = array(
'#type' => 'value',
'#value' => $language->id
);
}
else {
$form['langcode'] = array(
'#type' => 'textfield',
'#title' => t('Language code'),
'#maxlength' => 12,
'#required' => TRUE,
'#default_value' => '',
'#disabled' => FALSE,
'#description' => t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
);
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Language name in English'),
'#maxlength' => 64,
'#default_value' => @$language->name,
'#required' => TRUE,
);
$form['direction'] = array(
'#type' => 'radios',
'#title' => t('Direction'),
'#required' => TRUE,
'#description' => t('Direction that text in this language is presented.'),
'#default_value' => @$language->direction,
'#options' => array(Language::DIRECTION_LTR => t('Left to right'), Language::DIRECTION_RTL => t('Right to left')),
);
return $form;
}
/**
* Element specific validator for the Add language button.
*/
function language_admin_add_predefined_form_validate($form, &$form_state) {
$langcode = $form_state['values']['predefined_langcode'];
if ($langcode == 'custom') {
form_set_error('predefined_langcode', t('Fill in the language details and save the language with <em>Add custom language</em>.'));
}
else {
if ($language = language_load($langcode)) {
form_set_error('predefined_langcode', t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
}
}
}
/**
* Validate the language addition form on custom language button.
*/
function language_admin_add_custom_form_validate($form, &$form_state) {
if ($form_state['values']['predefined_langcode'] == 'custom') {
$langcode = $form_state['values']['langcode'];
// Reuse the editing form validation routine if we add a custom language.
language_admin_edit_form_validate($form['custom_language'], $form_state);
if ($language = language_load($langcode)) {
form_set_error('langcode', t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
}
}
else {
form_set_error('predefined_langcode', t('Use the <em>Add language</em> button to save a predefined language.'));
}
}
/**
* Process the custom and predefined language addition form submission.
*/
function language_admin_add_form_submit($form, &$form_state) {
$langcode = $form_state['values']['predefined_langcode'];
if ($langcode == 'custom') {
$langcode = $form_state['values']['langcode'];
// Custom language form.
$language = new Language(array(
'id' => $langcode,
'name' => $form_state['values']['name'],
'direction' => $form_state['values']['direction'],
));
}
else {
$language = new Language(array('id' => $langcode));
}
// Save the language and inform the user that it happened.
$language = language_save($language);
drupal_set_message(t('The language %language has been created and can now be used.', array('%language' => $language->name)));
// Tell the user they have the option to add a language switcher block
// to their theme so they can switch between the languages.
drupal_set_message(t('Use one of the language switcher blocks to allow site visitors to switch between languages. You can enable these blocks on the <a href="@block-admin">block administration page</a>.', array('@block-admin' => url('admin/structure/block'))));
$form_state['redirect'] = 'admin/config/regional/language';
}
/**
* Validate the language editing form. Reused for custom language addition too.
*/
function language_admin_edit_form_validate($form, &$form_state) {
// Ensure sane field values for langcode and name.
if (!isset($form['langcode_view']) && preg_match('@[^a-zA-Z_-]@', $form_state['values']['langcode'])) {
form_set_error('langcode', t('%field may only contain characters a-z, underscores, or hyphens.', array('%field' => $form['langcode']['#title'])));
}
if ($form_state['values']['name'] != check_plain($form_state['values']['name'])) {
form_set_error('name', t('%field cannot contain any markup.', array('%field' => $form['name']['#title'])));
}
}
/**
* Process the language editing form submission.
*/
function language_admin_edit_form_submit($form, &$form_state) {
// Prepare a language object for saving.
$languages = language_list();
$langcode = $form_state['values']['langcode'];
$language = $languages[$langcode];
$language->name = $form_state['values']['name'];
$language->direction = $form_state['values']['direction'];
language_save($language);
$form_state['redirect'] = 'admin/config/regional/language';
}
/**
* Prepare a language code list for unused predefined languages.
......
language_add_local_action:
route_name: language_add
title: 'Add language'
appears_on:
- language_admin_overview
......@@ -78,20 +78,12 @@ function language_menu() {
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/regional/language/add'] = array(
'title' => 'Add language',
'page callback' => 'drupal_get_form',
'page arguments' => array('language_admin_add_form'),
'access arguments' => array('administer languages'),
'type' => MENU_LOCAL_ACTION,
'file' => 'language.admin.inc',
'route_name' => 'language_add',
'type' => MENU_SIBLING_LOCAL_TASK,
);
$items['admin/config/regional/language/edit/%language'] = array(
'title' => 'Edit language',
'page callback' => 'drupal_get_form',
'page arguments' => array('language_admin_edit_form', 5),
'access callback' => 'language_access_language_edit_or_delete',
'access arguments' => array(5),
'file' => 'language.admin.inc',
'route_name' => 'language_edit',
);
$items['admin/config/regional/language/delete/%language'] = array(
'route_name' => 'language_delete',
......
......@@ -19,6 +19,20 @@ language_negotiation_selected:
requirements:
_permission: 'administer languages'
language_add:
pattern: '/admin/config/regional/language/add'
defaults:
_entity_form: 'language_entity.add'
requirements:
_entity_create_access: 'language_entity'
language_edit:
pattern: '/admin/config/regional/language/edit/{language_entity}'
defaults:
_entity_form: 'language_entity.edit'
requirements:
_entity_access: 'language_entity.update'
language_admin_overview:
pattern: '/admin/config/regional/language'
defaults:
......
......@@ -26,6 +26,8 @@
* "list" = "Drupal\language\LanguageListController",
* "access" = "Drupal\language\LanguageAccessController",
* "form" = {
* "add" = "Drupal\language\Form\LanguageAddForm",
* "edit" = "Drupal\language\Form\LanguageEditForm",
* "delete" = "Drupal\language\Form\LanguageDeleteForm"
* }
* },
......
<?php
/**
* @file
* Contains \Drupal\language\Form\LanguageAddForm.
*/
namespace Drupal\language\Form;
use Drupal\language\Form\LanguageFormBase;
use Drupal\Core\Language\Language;
/**
* Controller for language addition forms.
*/
class LanguageAddForm extends LanguageFormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
// @todo Remove in favour of base method.
return 'language_admin_add_form';
}
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$form['#title'] = $this->t('Add language');
$this->moduleHandler->loadInclude('language', 'inc', 'language.admin');
$predefined_languages = language_admin_predefined_list();
$predefined_languages['custom'] = $this->t('Custom language...');
$predefined_default = !empty($form_state['values']['predefined_langcode']) ? $form_state['values']['predefined_langcode'] : key($predefined_languages);
$form['predefined_langcode'] = array(
'#type' => 'select',
'#title' => $this->t('Language name'),
'#default_value' => $predefined_default,
'#options' => $predefined_languages,
);
$form['predefined_submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Add language'),
'#limit_validation_errors' => array(array('predefined_langcode'), array('predefined_submit')),
'#states' => array(
'invisible' => array(
'select#edit-predefined-langcode' => array('value' => 'custom'),
),
),
'#validate' => array(array($this, 'validatePredefined')),
'#submit' => array(array($this, 'submitForm')),
);
$form['custom_language'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
'select#edit-predefined-langcode' => array('value' => 'custom'),
),
),
);
$this->commonForm($form['custom_language']);
$form['custom_language']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Add custom language'),
'#validate' => array(array($this, 'validateCustom')),
'#submit' => array(array($this, 'submitForm')),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$langcode = $form_state['values']['predefined_langcode'];
if ($langcode == 'custom') {
$langcode = $form_state['values']['langcode'];
// Custom language form.
$language = new Language(array(
'id' => $langcode,
'name' => $form_state['values']['name'],
'direction' => $form_state['values']['direction'],
));
}
else {
$language = new Language(array('id' => $langcode));
}
// Save the language and inform the user that it happened.
$language = language_save($language);
drupal_set_message($this->t('The language %language has been created and can now be used.', array('%language' => $language->name)));
// Tell the user they have the option to add a language switcher block
// to their theme so they can switch between the languages.
drupal_set_message($this->t('Use one of the language switcher blocks to allow site visitors to switch between languages. You can enable these blocks on the <a href="@block-admin">block administration page</a>.', array('@block-admin' => url('admin/structure/block'))));
$form_state['redirect'] = 'admin/config/regional/language';
}
/**
* {@inheritdoc}
*/
public function actions(array $form, array &$form_state) {
// No actions needed.
return array();
}
/**
* Validates the language addition form on custom language button.
*/
public function validateCustom(array $form, array &$form_state) {
if ($form_state['values']['predefined_langcode'] == 'custom') {
$langcode = $form_state['values']['langcode'];
// Reuse the editing form validation routine if we add a custom language.
$this->validateCommon($form['custom_language'], $form_state);
if ($language = language_load($langcode)) {
form_error($form['custom_language']['langcode'], $this->t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
}
}
else {
form_error($form['predefined_langcode'], $this->t('Use the <em>Add language</em> button to save a predefined language.'));
}
}
/**
* Element specific validator for the Add language button.
*/
public function validatePredefined($form, &$form_state) {
$langcode = $form_state['values']['predefined_langcode'];
if ($langcode == 'custom') {
form_error($form['predefined_langcode'], $this->t('Fill in the language details and save the language with <em>Add custom language</em>.'));
}
else {
if ($language = language_load($langcode)) {
form_error($form['predefined_langcode'], $this->t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
}
}
}
}
<?php
/**
* @file
* Contains \Drupal\language\Form\LanguageEditForm.
*/
namespace Drupal\language\Form;
use Drupal\language\Form\LanguageFormBase;
/**
* Controller for language edit forms.
*/
class LanguageEditForm extends LanguageFormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
// @todo Remove in favour of base method.
return 'language_admin_edit_form';
}
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$this->commonForm($form);
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function actions(array $form, array &$form_state) {
$actions['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save language'),
'#validate' => array(array($this, 'validateCommon')),
'#submit' => array(array($this, 'submitForm')),
);
return $actions;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
// Prepare a language object for saving.
$languages = language_list();
$langcode = $form_state['values']['langcode'];
$language = $languages[$langcode];
$language->name = $form_state['values']['name'];
$language->direction = $form_state['values']['direction'];
language_save($language);
$form_state['redirect'] = 'admin/config/regional/language';
}
}
<?php
/**
* @file
* Contains \Drupal\language\Form\LanguageFormBase.
*/
namespace Drupal\language\Form;
use Drupal\Core\Entity\EntityFormController;
use Drupal\Core\Language\Language;
/**
* Base form controller for language add and edit forms.
*/
abstract class LanguageFormBase extends EntityFormController {
/**
* Common elements of the language addition and editing form.
*/
public function commonForm(array &$form) {
$language = $this->entity;
if (isset($language->id)) {
$form['langcode_view'] = array(
'#type' => 'item',
'#title' => $this->t('Language code'),
'#markup' => $language->id
);
$form['langcode'] = array(
'#type' => 'value',
'#value' => $language->id
);
}
else {
$form['langcode'] = array(
'#type' => 'textfield',
'#title' => $this->t('Language code'),
'#maxlength' => 12,
'#required' => TRUE,
'#default_value' => '',
'#disabled' => FALSE,
'#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>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
);
}
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Language name in English'),
'#maxlength' => 64,
'#default_value' => $language->label,
'#required' => TRUE,
);
$form['direction'] = array(
'#type' => 'radios',
'#title' => $this->t('Direction'),
'#required' => TRUE,
'#description' => $this->t('Direction that text in this language is presented.'),
'#default_value' => $language->direction,
'#options' => array(
Language::DIRECTION_LTR => $this->t('Left to right'),
Language::DIRECTION_RTL => $this->t('Right to left'),
),
);
return $form;
}
/**
* Validates the language editing element.
*/
public function validateCommon(array $form, array &$form_state) {
// Ensure sane field values for langcode and name.
if (!isset($form['langcode_view']) && preg_match('@[^a-zA-Z_-]@', $form_state['values']['langcode'])) {
form_error($form['langcode'], $this->t('%field may only contain characters a-z, underscores, or hyphens.', array('%field' => $form['langcode']['#title'])));
}
if ($form_state['values']['name'] != check_plain($form_state['values']['name'])) {
form_error($form['name'], $this->t('%field cannot contain any markup.', array('%field' => $form['name']['#title'])));
}
}
}
......@@ -19,7 +19,6 @@ class LanguageAccessController extends EntityAccessController {
*/
public function access(EntityInterface $entity, $operation, $langcode = Language::LANGCODE_DEFAULT, AccountInterface $account = NULL) {
switch ($operation) {
case 'create':
case 'update':
case 'delete':
return !$entity->locked && user_access('administer languages');
......@@ -28,4 +27,11 @@ public function access(EntityInterface $entity, $operation, $langcode = Language
return FALSE;
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return $account->hasPermission('administer languages');
}
}
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\language\Entity\LanguageInterface.
* Contains \Drupal\language\LanguageInterface.
*/
namespace Drupal\language;
......
......@@ -819,7 +819,7 @@ function locale_form_language_admin_edit_form_alter(&$form, &$form_state) {
'#type' => 'checkbox',
'#default_value' => locale_translate_english(),
);
$form['#submit'][] = 'locale_form_language_admin_edit_form_alter_submit';
$form['actions']['submit']['#submit'][] = 'locale_form_language_admin_edit_form_alter_submit';
}
}
......
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