Commit 206e2b25 authored by webchick's avatar webchick

Issue #2072941 by marcingy, tim.plunkett: Modernize block.module forms.

parent d6693eb3
......@@ -7,10 +7,12 @@
namespace Drupal\block;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityFormController;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -18,6 +20,13 @@
*/
class BlockFormController extends EntityFormController {
/**
* The block entity.
*
* @var \Drupal\block\BlockInterface
*/
protected $entity;
/**
* The block storage controller.
*
......@@ -32,6 +41,13 @@ class BlockFormController extends EntityFormController {
*/
protected $entityQueryFactory;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* Constructs a BlockFormController object.
*
......@@ -39,10 +55,13 @@ class BlockFormController extends EntityFormController {
* The entity manager.
* @param \Drupal\Core\Entity\Query\QueryFactory $entity_query_factory
* The entity query factory.
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
*/
public function __construct(EntityManager $entity_manager, QueryFactory $entity_query_factory) {
public function __construct(EntityManager $entity_manager, QueryFactory $entity_query_factory, LanguageManager $language_manager) {
$this->storageController = $entity_manager->getStorageController('block');
$this->entityQueryFactory = $entity_query_factory;
$this->languageManager = $language_manager;
}
/**
......@@ -51,12 +70,13 @@ public function __construct(EntityManager $entity_manager, QueryFactory $entity_
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity'),
$container->get('entity.query')
$container->get('entity.query'),
$container->get('language_manager')
);
}
/**
* Overrides \Drupal\Core\Entity\EntityFormController::form().
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$entity = $this->entity;
......@@ -68,16 +88,12 @@ public function form(array $form, array &$form_state) {
$form['settings'] = $entity->getPlugin()->buildConfigurationForm(array(), $form_state);
// If creating a new block, calculate a safe default machine name.
if ($entity->isNew()) {
$machine_default = $this->getUniqueMachineName($entity);
}
$form['machine_name'] = array(
'#type' => 'machine_name',
'#title' => t('Machine name'),
'#title' => $this->t('Machine name'),
'#maxlength' => 64,
'#description' => t('A unique name for this block instance. Must be alpha-numeric and underscore separated.'),
'#default_value' => !$entity->isNew() ? $entity->id() : $machine_default,
'#description' => $this->t('A unique name for this block instance. Must be alpha-numeric and underscore separated.'),
'#default_value' => !$entity->isNew() ? $entity->id() : $this->getUniqueMachineName($entity),
'#machine_name' => array(
'exists' => 'block_load',
'replace_pattern' => '[^a-z0-9_.]+',
......@@ -90,7 +106,7 @@ public function form(array $form, array &$form_state) {
// Visibility settings.
$form['visibility'] = array(
'#type' => 'vertical_tabs',
'#title' => t('Visibility settings'),
'#title' => $this->t('Visibility settings'),
'#attached' => array(
'js' => array(drupal_get_path('module', 'block') . '/block.js'),
),
......@@ -102,7 +118,7 @@ public function form(array $form, array &$form_state) {
// Per-path visibility.
$form['visibility']['path'] = array(
'#type' => 'details',
'#title' => t('Pages'),
'#title' => $this->t('Pages'),
'#collapsed' => TRUE,
'#group' => 'visibility',
'#weight' => 0,
......@@ -112,7 +128,7 @@ public function form(array $form, array &$form_state) {
// this entire visibility settings section probably needs a separate user
// interface in the near future.
$visibility = $entity->get('visibility');
$access = user_access('use PHP for settings');
$access = $this->getCurrentUser()->hasPermission('use PHP for settings');
if (!empty($visibility['path']['visibility']) && $visibility['path']['visibility'] == BLOCK_VISIBILITY_PHP && !$access) {
$form['visibility']['path']['visibility'] = array(
'#type' => 'value',
......@@ -125,22 +141,22 @@ public function form(array $form, array &$form_state) {
}
else {
$options = array(
BLOCK_VISIBILITY_NOTLISTED => t('All pages except those listed'),
BLOCK_VISIBILITY_LISTED => t('Only the listed pages'),
BLOCK_VISIBILITY_NOTLISTED => $this->t('All pages except those listed'),
BLOCK_VISIBILITY_LISTED => $this->t('Only the listed pages'),
);
$description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %user for the current user's page and %user-wildcard for every user page. %front is the front page.", array('%user' => 'user', '%user-wildcard' => 'user/*', '%front' => '<front>'));
$description = $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %user for the current user's page and %user-wildcard for every user page. %front is the front page.", array('%user' => 'user', '%user-wildcard' => 'user/*', '%front' => '<front>'));
if (module_exists('php') && $access) {
$options += array(BLOCK_VISIBILITY_PHP => t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
$title = t('Pages or PHP code');
$description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
if ($this->moduleHandler->moduleExists('php') && $access) {
$options += array(BLOCK_VISIBILITY_PHP => $this->t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
$title = $this->t('Pages or PHP code');
$description .= ' ' . $this->t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
}
else {
$title = t('Pages');
$title = $this->t('Pages');
}
$form['visibility']['path']['visibility'] = array(
'#type' => 'radios',
'#title' => t('Show block on specific pages'),
'#title' => $this->t('Show block on specific pages'),
'#options' => $options,
'#default_value' => !empty($visibility['path']['visibility']) ? $visibility['path']['visibility'] : BLOCK_VISIBILITY_NOTLISTED,
);
......@@ -153,11 +169,12 @@ public function form(array $form, array &$form_state) {
}
// Configure the block visibility per language.
if (module_exists('language') && language_multilingual()) {
if ($this->moduleHandler->moduleExists('language') && $this->languageManager->isMultilingual()) {
$configurable_language_types = language_types_get_configurable();
// Fetch languages.
$languages = language_list(Language::STATE_ALL);
$langcodes_options = array();
foreach ($languages as $language) {
// @todo $language->name is not wrapped with t(), it should be replaced
// by CMI translation implementation.
......@@ -165,14 +182,14 @@ public function form(array $form, array &$form_state) {
}
$form['visibility']['language'] = array(
'#type' => 'details',
'#title' => t('Languages'),
'#title' => $this->t('Languages'),
'#collapsed' => TRUE,
'#group' => 'visibility',
'#weight' => 5,
);
// If there are multiple configurable language types, let the user pick
// which one should be applied to this visibility setting. This way users
// can limit blocks by interface language or content language for exmaple.
// can limit blocks by interface language or content language for example.
$language_types = language_types_info();
$language_type_options = array();
foreach ($configurable_language_types as $type_key) {
......@@ -180,17 +197,17 @@ public function form(array $form, array &$form_state) {
}
$form['visibility']['language']['language_type'] = array(
'#type' => 'radios',
'#title' => t('Language type'),
'#title' => $this->t('Language type'),
'#options' => $language_type_options,
'#default_value' => !empty($visibility['language']['language_type']) ? $visibility['language']['language_type'] : $configurable_language_types[0],
'#access' => count($language_type_options) > 1,
);
$form['visibility']['language']['langcodes'] = array(
'#type' => 'checkboxes',
'#title' => t('Show this block only for specific languages'),
'#title' => $this->t('Show this block only for specific languages'),
'#default_value' => !empty($visibility['language']['langcodes']) ? $visibility['language']['langcodes'] : array(),
'#options' => $langcodes_options,
'#description' => t('Show this block only for the selected language(s). If you select no languages, the block will be visibile in all languages.'),
'#description' => $this->t('Show this block only for the selected language(s). If you select no languages, the block will be visible in all languages.'),
);
}
......@@ -198,24 +215,24 @@ public function form(array $form, array &$form_state) {
$role_options = array_map('check_plain', user_role_names());
$form['visibility']['role'] = array(
'#type' => 'details',
'#title' => t('Roles'),
'#title' => $this->t('Roles'),
'#collapsed' => TRUE,
'#group' => 'visibility',
'#weight' => 10,
);
$form['visibility']['role']['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Show block for specific roles'),
'#title' => $this->t('Show block for specific roles'),
'#default_value' => !empty($visibility['role']['roles']) ? $visibility['role']['roles'] : array(),
'#options' => $role_options,
'#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
'#description' => $this->t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
);
// Region settings.
$form['region'] = array(
'#type' => 'select',
'#title' => t('Region'),
'#description' => t('Select the region where this block should be displayed.'),
'#title' => $this->t('Region'),
'#description' => $this->t('Select the region where this block should be displayed.'),
'#default_value' => $entity->get('region'),
'#empty_value' => BLOCK_REGION_NONE,
'#options' => system_region_list($entity->get('theme'), REGIONS_VISIBLE),
......@@ -224,16 +241,16 @@ public function form(array $form, array &$form_state) {
}
/**
* Overrides \Drupal\Core\Entity\EntityFormController::actions().
* {@inheritdoc}
*/
protected function actions(array $form, array &$form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = t('Save block');
$actions['submit']['#value'] = $this->t('Save block');
return $actions;
}
/**
* Overrides \Drupal\Core\Entity\EntityFormController::validate().
* {@inheritdoc}
*/
public function validate(array $form, array &$form_state) {
parent::validate($form, $form_state);
......@@ -257,7 +274,7 @@ public function validate(array $form, array &$form_state) {
}
/**
* Overrides \Drupal\Core\Entity\EntityFormController::submit().
* {@inheritdoc}
*/
public function submit(array $form, array &$form_state) {
parent::submit($form, $form_state);
......@@ -274,8 +291,8 @@ public function submit(array $form, array &$form_state) {
// Save the settings of the plugin.
$entity->save();
drupal_set_message(t('The block configuration has been saved.'));
cache_invalidate_tags(array('content' => TRUE));
drupal_set_message($this->t('The block configuration has been saved.'));
Cache::invalidateTags(array('content' => TRUE));
$form_state['redirect'] = 'admin/structure/block/list/' . $entity->get('theme');
}
......
......@@ -18,7 +18,7 @@ class BlockDeleteForm extends EntityConfirmFormBase {
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete the block %name?', array('%name' => $this->entity->label()));
return $this->t('Are you sure you want to delete the block %name?', array('%name' => $this->entity->label()));
}
/**
......@@ -32,7 +32,7 @@ public function getCancelPath() {
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Delete');
return $this->t('Delete');
}
/**
......@@ -40,7 +40,7 @@ public function getConfirmText() {
*/
public function submit(array $form, array &$form_state) {
$this->entity->delete();
drupal_set_message(t('The block %name has been removed.', array('%name' => $this->entity->label())));
drupal_set_message($this->t('The block %name has been removed.', array('%name' => $this->entity->label())));
$form_state['redirect'] = 'admin/structure/block';
}
......
......@@ -78,7 +78,11 @@ public function testGetUniqueMachineName() {
->method('getStorageController')
->will($this->returnValue($block_storage));
$block_form_controller = new BlockFormController($entity_manager, $query_factory);
$language_manager = $this->getMockBuilder('Drupal\Core\Language\LanguageManager')
->disableOriginalConstructor()
->getMock();
$block_form_controller = new BlockFormController($entity_manager, $query_factory, $language_manager);
// Ensure that the block with just one other instance gets the next available
// name suggestion.
......
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