diff --git a/core/modules/contact/src/ContactFormListBuilder.php b/core/modules/contact/src/ContactFormListBuilder.php index 1c16d51..369249a 100644 --- a/core/modules/contact/src/ContactFormListBuilder.php +++ b/core/modules/contact/src/ContactFormListBuilder.php @@ -10,6 +10,7 @@ use Drupal\Component\Utility\String; use Drupal\Core\Config\Entity\ConfigEntityListBuilder; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Routing\LinkGeneratorTrait; use Drupal\Core\Url; /** @@ -19,6 +20,8 @@ */ class ContactFormListBuilder extends ConfigEntityListBuilder { + use LinkGeneratorTrait; + /** * {@inheritdoc} */ @@ -33,15 +36,14 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - $row['form'] = $this->getLabel($entity); // Special case the personal form. if ($entity->id() == 'personal') { - $row['category'] = $this->getLabel($entity); + $row['form'] = $this->getLabel($entity); $row['recipients'] = t('Selected user'); $row['selected'] = t('No'); } else { - $row['category'] = $this->l($this->getLabel($entity),'contact.site_page_form', array('contact_form' => $entity->id())); + $row['form'] = $this->l($this->getLabel($entity), Url::fromRoute('contact.site_page_form', ['contact_form' => $entity->id()])); $row['recipients'] = String::checkPlain(implode(', ', $entity->getRecipients())); $default_form = \Drupal::config('contact.settings')->get('default_form'); $row['selected'] = ($default_form == $entity->id() ? t('Yes') : t('No')); @@ -49,30 +51,4 @@ public function buildRow(EntityInterface $entity) { return $row + parent::buildRow($entity); } - /** - * Renders a link to a route given a route name and its parameters. - * - * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details - * on the arguments, usage, and possible exceptions. - * - * @return string - * An HTML string containing a link to the given route and parameters. - */ - protected function l($text, $route_name, array $parameters = array(), array $options = array()) { - return $this->linkGenerator()->generate($text, new Url($route_name, $parameters, $options)); - } - - /** - * Returns the link generator. - * - * @return \Drupal\Core\Utility\LinkGeneratorInterface - * The link generator - */ - protected function linkGenerator() { - if (!isset($this->linkGenerator)) { - $this->linkGenerator = \Drupal::linkGenerator(); - } - return $this->linkGenerator; - } - } diff --git a/core/modules/contact/src/Plugin/Block/ContactNavigationBlock.php b/core/modules/contact/src/Plugin/Block/ContactNavigationBlock.php index 7e921e5..9f0c22e 100644 --- a/core/modules/contact/src/Plugin/Block/ContactNavigationBlock.php +++ b/core/modules/contact/src/Plugin/Block/ContactNavigationBlock.php @@ -7,60 +7,50 @@ namespace Drupal\contact\Plugin\Block; -use Drupal\block\BlockBase; -use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\String; +use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; use Drupal\Core\Config\Entity\ConfigEntityStorageInterface; -use Drupal\Core\Form\FormBuilderInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a 'Contact categories' block. + * Provides a 'Contact forms' block. * * @Block( * id = "contact_navigation", - * admin_label = @Translation("Contact categories"), + * admin_label = @Translation("Contact forms"), * category = @Translation("Menus") * ) */ class ContactNavigationBlock extends BlockBase implements ContainerFactoryPluginInterface { /** - * The contact category storage. + * The contact form storage. * * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface */ protected $storage; /** - * The form builder. - * - * @var \Drupal\Core\Form\FormBuilderInterface - */ - protected $formBuilder; - - /** * Constructs a new ContactNavigationBlock instance. * * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. - * @param array $plugin_definition + * @param mixed $plugin_definition * The plugin implementation definition. * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage * The contact category storage. - * @param $form_builder \Drupal\Core\Form\FormBuilderInterface - * The form builder. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigEntityStorageInterface $storage, FormBuilderInterface $form_builder) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigEntityStorageInterface $storage) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->storage = $storage; - $this->formBuilder = $form_builder; } /** @@ -71,8 +61,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager')->getStorage('contact_category'), - $container->get('form_builder') + $container->get('entity.manager')->getStorage('contact_form') ); } @@ -80,22 +69,23 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function defaultConfiguration() { - return array( - 'categories' => array(), - 'cache' => array('max_age' => Cache::PERMANENT) - ); + return [ + 'forms' => [], + 'cache' => ['max_age' => Cache::PERMANENT], + ]; } /** * {@inheritdoc} */ public function getCacheTags() { - // Even when the menu block renders to the empty string for a user, we want - // the cache tag for this menu to be set: whenever the menu is changed, this - // menu block must also be re-rendered for that user, because maybe a menu - // link that is accessible for that user has been added. - $tags = array('contact_category' => array_keys($this->configuration['categories'])); - return NestedArray::mergeDeep(parent::getCacheTags(), $tags); + $cache_tags = parent::getCacheTags(); + if ($forms = array_keys($this->configuration['forms'])) { + foreach ($this->storage->loadMultiple($forms) as $form) { + $cache_tags = Cache::mergeTags($cache_tags, $form->getCacheTag()); + } + } + return $cache_tags; } /** @@ -104,13 +94,13 @@ public function getCacheTags() { protected function getRequiredCacheContexts() { // This blocks must be cached per role: different roles may have access to // contact forms. - return array('cache_context.user.roles'); + return ['cache_context.user.roles']; } /** * {@inheritdoc} */ - public function access(AccountInterface $account) { + protected function blockAccess(AccountInterface $account) { // Only grant access to users with the 'access site-wide contact form' // permission. return $account->hasPermission('access site-wide contact form'); @@ -119,72 +109,72 @@ public function access(AccountInterface $account) { /** * {@inheritdoc} */ - function blockForm($form, &$form_state) { - $form['categories'] = array( + public function blockForm($form, FormStateInterface $form_state) { + $form['forms'] = [ '#type' => 'table', - '#header' => array( - 'label' => $this->t('Category name'), + '#header' => [ + 'label' => $this->t('Form'), 'display' => $this->t('Display'), 'weight' => $this->t('Weight'), - ), - '#empty' => $this->t('There is no contact categories yet. Create one.'), - '#tabledrag' => array( - array( + ], + '#empty' => $this->t('There is no contact forms yet. Create one.'), + '#tabledrag' => [ + [ 'action' => 'order', 'relationship' => 'sibling', 'group' => 'weight', - ), - ), - ); + ], + ], + ]; - $categories = $this->configuration['categories']; - $rows = array(); + $contact_forms = $this->configuration['forms']; + $rows = []; $min_weight = 0; - /** @var \Drupal\contact\CategoryInterface $category */ - foreach ($this->storage->loadMultiple() as $id => $category) { - // Do not list personal category. + /** @var \Drupal\contact\ContactFormInterface $contact_form */ + foreach ($this->storage->loadMultiple() as $id => $contact_form) { + // Do not list personal contact form. if ($id == 'personal') { continue; } - $row_label = $category->label(); - $row_weight = $category->get('weight'); + $row_label = $contact_form->label(); + $row_weight = $contact_form->getWeight(); $row_display = TRUE; - if ($categories) { - // Display only selected categories by default if there are any. - $row_display = isset($categories[$id]); - // Find lowest weight to display selected categories first. + if ($contact_forms) { + // Display only selected forms by default if there are any. + $row_display = isset($contact_forms[$id]); + // Find lowest weight to display selected forms first. if ($min_weight > $row_weight) { $min_weight = $row_weight; } } - $row = array( - 'label' => array('#markup' => String::checkPlain($row_label)), - 'display' => array( + $row = [ + 'label' => ['#markup' => String::checkPlain($row_label)], + 'display' => [ '#type' => 'checkbox', '#default_value' => $row_display, - '#title' => t('Display the @title', array('@title' => $row_label)), + '#title' => t('Display the @title', ['@title' => $row_label]), '#title_display' => 'invisible', - ), + ], // Add weight column. - 'weight' => array( + 'weight' => [ '#type' => 'weight', - '#title' => $this->t('Weight for @title', array('@title' => $row_label)), + '#title' => $this->t('Weight for @title', ['@title' => $row_label]), '#title_display' => 'invisible', '#default_value' => $row_weight, - '#attributes' => array('class' => array('weight')), - ), - ); + '#attributes' => ['class' => ['weight']], + ], + ]; $row['#attributes']['class'][] = 'draggable'; $row['#weight'] = $row_weight; - $rows[$category->id()] = $row; + $rows[$contact_form->id()] = $row; } - if ($categories) { + if ($contact_forms) { // Move selected categories to the top of the list. - $min_weight = $min_weight - count($categories); - foreach ($categories as $id => $data) { + $min_weight = $min_weight - count($contact_forms); + foreach ($contact_forms as $id => $data) { if (isset($rows[$id])) { $rows[$id]['#weight'] = $min_weight; $rows[$id]['weight']['#default_value'] = $min_weight++; @@ -193,7 +183,7 @@ function blockForm($form, &$form_state) { } uasort($rows, '\Drupal\Component\Utility\SortArray::sortByWeightProperty'); - $form['categories'] += $rows; + $form['forms'] += $rows; return $form; } @@ -201,44 +191,43 @@ function blockForm($form, &$form_state) { /** * {@inheritdoc} */ - public function blockValidate($form, &$form_state) { - $categories = array(); - foreach ($form_state['values']['categories'] as $id => $value) { + public function blockValidate($form, FormStateInterface $form_state) { + $contact_forms = []; + foreach ($form_state->getValue('forms') as $id => $value) { if ($value['display']) { - $categories[$id] = $value['weight']; + $contact_forms[$id] = $value['weight']; } } - if (empty($categories)) { - $this->formBuilder->setErrorByName('categories', $form_state, $this->t('At least one category should be selected.')); + if (empty($contact_forms)) { + $form_state->setErrorByName('forms', $this->t('At least one category should be selected.')); } - $form_state['values']['categories'] = $categories; + $form_state->setValue('forms', $contact_forms); } /** * {@inheritdoc} */ - public function blockSubmit($form, &$form_state) { - $this->configuration['categories'] = $form_state['values']['categories']; + public function blockSubmit($form, FormStateInterface $form_state) { + $this->configuration['forms'] = $form_state->getValue('forms'); } /** * {@inheritdoc} */ public function build() { - $links = array(); - /** @var \Drupal\contact\CategoryInterface $category */ - foreach ($this->storage->loadMultiple(array_keys($this->configuration['categories'])) as $id => $category) { - $links[$id] = array( + $links = []; + /** @var \Drupal\contact\ContactFormInterface $contact_form */ + foreach ($this->storage->loadMultiple(array_keys($this->configuration['forms'])) as $id => $contact_form) { + $links[$id] = [ '#type' => 'link', - '#title' => $category->label(), - '#route_name' => 'contact.site_page_form', - '#route_parameters' => array('contact_form' => $id), - ); + '#title' => $contact_form->label(), + '#url' => Url::fromRoute('contact.site_page_form', ['contact_form' => $id]), + ]; } - return array( + return [ '#theme' => 'item_list__contact', '#items' => $links, - ); + ]; } }