diff --git a/lib/Drupal/views/Plugin/views/area/Title.php b/lib/Drupal/views/Plugin/views/area/Title.php new file mode 100644 index 0000000000000000000000000000000000000000..e9185a009dec458a57d96226ec6ef6cee2ded9b8 --- /dev/null +++ b/lib/Drupal/views/Plugin/views/area/Title.php @@ -0,0 +1,59 @@ +<?php + +/** + * @file + * Definition of Drupal\views\Plugin\views\area\Title. + */ + +namespace Drupal\views\Plugin\views\area; + +use Drupal\Core\Annotation\Plugin; + +/** + * Views area title override handler. + * + * @ingroup views_area_handlers + * + * @Plugin( + * id = "title" + * ) + */ +class Title extends AreaPluginBase { + + /** + * Overrides Drupal\views\Plugin\views\AreaPluginBase::defineOptions(). + */ + protected function defineOptions() { + $options = parent::defineOptions(); + $options['title'] = array('default' => '', 'translatable' => TRUE); + + return $options; + } + + /** + * Overrides Drupal\views\Plugin\views\AreaPluginBase::buildOptionsForm(). + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Overridden title'), + '#default_value' => $this->options['title'], + '#description' => t('Override the title of this view when it is empty.'), + ); + + } + + /** + * Overrides Drupal\views\Plugin\views\AreaPluginBase::render(). + */ + function render($empty = FALSE) { + if (!empty($this->options['title'])) { + $this->view->setTitle(filter_xss_admin($this->options['title']), PASS_THROUGH); + } + + return ''; + } + +} diff --git a/lib/Drupal/views/Tests/Handler/AreaTest.php b/lib/Drupal/views/Tests/Handler/AreaTest.php index 7249562cfb71fd633ff9d5de0897b4cc1845a573..085406ba2ff4cb889de74b0cd1d3fcc1c9e52f7c 100644 --- a/lib/Drupal/views/Tests/Handler/AreaTest.php +++ b/lib/Drupal/views/Tests/Handler/AreaTest.php @@ -104,4 +104,30 @@ public function testRenderArea() { $this->assertTrue(strpos($output, $empty_string) !== FALSE); } + /** + * Tests overriding the view title using the area title handler. + */ + public function testTitleArea() { + $view = views_get_view('frontpage'); + $view->initDisplay('page_1'); + + // Add the title area handler to the empty area. + $view->displayHandlers['page_1']->overrideOption('empty', array( + 'title' => array( + 'id' => 'title', + 'table' => 'views', + 'field' => 'title', + 'admin_label' => '', + 'label' => '', + 'empty' => '0', + 'title' => 'Overridden title', + ), + )); + + $view->storage->enable(); + + $this->drupalGet('frontpage'); + $this->assertText('Overridden title', 'Overridden title found.'); + } + } diff --git a/modules/views.views.inc b/modules/views.views.inc index a12eddeb8da8772b0782537820d4a796047a76b0..baee98ca352c5f6c53251d11e73850c884a5575c 100644 --- a/modules/views.views.inc +++ b/modules/views.views.inc @@ -65,6 +65,14 @@ function views_views_data() { ), ); + $data['views']['title'] = array( + 'title' => t('Title override'), + 'help' => t('Override the default view title for this view. This is useful to display an alternative title when a view is empty.'), + 'area' => array( + 'id' => 'title', + ), + ); + $data['views']['view'] = array( 'title' => t('View area'), 'help' => t('Insert a view inside an area.'),