2009-05-17 11:16:51 +00:00
< ? php
/**
* @ file
* Provide structure for the administrative interface to Views .
*/
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface ;
2014-10-09 06:39:37 +00:00
use Drupal\Core\Url ;
2012-09-18 19:41:58 +00:00
use Drupal\views\ViewExecutable ;
2012-09-26 20:02:06 +00:00
use Drupal\views\Analyzer ;
2012-06-16 10:45:11 +00:00
2013-12-31 23:10:47 +00:00
/**
* Implements hook_help () .
*/
2014-06-30 03:33:08 +00:00
function views_ui_help ( $route_name , RouteMatchInterface $route_match ) {
2014-05-07 02:04:53 +00:00
switch ( $route_name ) {
case 'help.page.views_ui' :
2013-12-31 23:10:47 +00:00
$output = '' ;
$output .= '<h3>' . t ( 'About' ) . '</h3>' ;
2019-04-16 05:38:27 +00:00
$output .= '<p>' . t ( 'The Views UI module provides an interface for managing views for the <a href=":views">Views module</a>. For more information, see the <a href=":handbook">online documentation for the Views UI module</a>.' , [ ':views' => Url :: fromRoute ( 'help.page' , [ 'name' => 'views' ]) -> toString (), ':handbook' => 'https://www.drupal.org/documentation/modules/views_ui' ]) . '</p>' ;
2013-12-31 23:10:47 +00:00
$output .= '<h3>' . t ( 'Uses' ) . '</h3>' ;
$output .= '<dl>' ;
2015-10-06 18:33:04 +00:00
$output .= '<dt>' . t ( 'Creating and managing views' ) . '</dt>' ;
2019-04-16 05:38:27 +00:00
$output .= '<dd>' . t ( 'Views can be created from the <a href=":list">Views list page</a> by using the "Add view" action. Existing views can be managed from the <a href=":list">Views list page</a> by locating the view in the "Enabled" or "Disabled" list and selecting the desired operation action, for example "Edit".' , [ ':list' => Url :: fromRoute ( 'entity.view.collection' , [ 'name' => 'views_ui' ]) -> toString ()]) . '</dd>' ;
2015-10-06 18:33:04 +00:00
$output .= '<dt>' . t ( 'Enabling and disabling views' ) . '<dt>' ;
2019-04-16 05:38:27 +00:00
$output .= '<dd>' . t ( 'Views can be enabled or disabled from the <a href=":list">Views list page</a>. To enable a view, find the view within the "Disabled" list and select the "Enable" operation. To disable a view find the view within the "Enabled" list and select the "Disable" operation.' , [ ':list' => Url :: fromRoute ( 'entity.view.collection' , [ 'name' => 'views_ui' ]) -> toString ()]) . '</dd>' ;
2015-10-06 18:33:04 +00:00
$output .= '<dt>' . t ( 'Exporting and importing views' ) . '</dt>' ;
2019-04-16 05:38:27 +00:00
$output .= '<dd>' . t ( 'Views can be exported and imported as configuration files by using the <a href=":config">Configuration Manager module</a>.' , [ ':config' => ( \Drupal :: moduleHandler () -> moduleExists ( 'config' )) ? Url :: fromRoute ( 'help.page' , [ 'name' => 'config' ]) -> toString () : '#' ]) . '</dd>' ;
2013-12-31 23:10:47 +00:00
$output .= '</dl>' ;
return $output ;
}
}
2013-05-03 17:25:44 +00:00
/**
2014-02-20 10:04:54 +00:00
* Implements hook_entity_type_build () .
2013-05-03 17:25:44 +00:00
*/
2014-02-20 10:04:54 +00:00
function views_ui_entity_type_build ( array & $entity_types ) {
2021-05-20 20:47:18 +00:00
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
2014-02-20 10:04:54 +00:00
$entity_types [ 'view' ]
2014-04-25 23:12:39 +00:00
-> setFormClass ( 'edit' , 'Drupal\views_ui\ViewEditForm' )
-> setFormClass ( 'add' , 'Drupal\views_ui\ViewAddForm' )
-> setFormClass ( 'preview' , 'Drupal\views_ui\ViewPreviewForm' )
2014-06-11 10:35:46 +00:00
-> setFormClass ( 'duplicate' , 'Drupal\views_ui\ViewDuplicateForm' )
2015-01-30 10:46:14 +00:00
-> setFormClass ( 'delete' , 'Drupal\Core\Entity\EntityDeleteForm' )
2014-01-22 07:32:29 +00:00
-> setFormClass ( 'break_lock' , 'Drupal\views_ui\Form\BreakLockForm' )
2014-03-23 22:25:46 +00:00
-> setListBuilderClass ( 'Drupal\views_ui\ViewListBuilder' )
2015-01-13 10:32:23 +00:00
-> setLinkTemplate ( 'edit-form' , '/admin/structure/views/view/{view}' )
-> setLinkTemplate ( 'edit-display-form' , '/admin/structure/views/view/{view}/edit/{display_id}' )
-> setLinkTemplate ( 'preview-form' , '/admin/structure/views/view/{view}/preview/{display_id}' )
-> setLinkTemplate ( 'duplicate-form' , '/admin/structure/views/view/{view}/duplicate' )
-> setLinkTemplate ( 'delete-form' , '/admin/structure/views/view/{view}/delete' )
-> setLinkTemplate ( 'enable' , '/admin/structure/views/view/{view}/enable' )
-> setLinkTemplate ( 'disable' , '/admin/structure/views/view/{view}/disable' )
2015-01-19 09:37:11 +00:00
-> setLinkTemplate ( 'break-lock-form' , '/admin/structure/views/view/{view}/break-lock' )
-> setLinkTemplate ( 'collection' , '/admin/structure/views' );
2013-05-03 17:25:44 +00:00
}
2009-05-17 11:16:51 +00:00
/**
* Implements hook_theme () .
*/
function views_ui_theme () {
2017-03-04 01:20:24 +00:00
return [
2009-05-17 11:16:51 +00:00
// edit a view
2017-03-04 01:20:24 +00:00
'views_ui_display_tab_setting' => [
'variables' => [ 'description' => '' , 'link' => '' , 'settings_links' => [], 'overridden' => FALSE , 'defaulted' => FALSE , 'description_separator' => TRUE , 'class' => []],
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
'views_ui_display_tab_bucket' => [
2009-05-17 11:16:51 +00:00
'render element' => 'element' ,
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
'views_ui_rearrange_filter_form' => [
2009-05-17 11:16:51 +00:00
'render element' => 'form' ,
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
'views_ui_expose_filter_form' => [
2009-05-17 11:16:51 +00:00
'render element' => 'form' ,
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
2009-05-17 11:16:51 +00:00
Issue #2574767 by dipakmdhrm, Manjit.Singh, malavya, Dom., yoroy, xjm, tkoleary, Gábor Hojtsy, dawehner, Cottser, eelkeblok, Bojhan, fgm, DuaelFr: Views listing page displays too few items on a page
2016-08-29 13:41:31 +00:00
// Legacy theme hook for displaying views info.
2017-03-04 01:20:24 +00:00
'views_ui_view_info' => [
'variables' => [ 'view' => NULL , 'displays' => NULL ],
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
2009-05-17 11:16:51 +00:00
Issue #2574767 by dipakmdhrm, Manjit.Singh, malavya, Dom., yoroy, xjm, tkoleary, Gábor Hojtsy, dawehner, Cottser, eelkeblok, Bojhan, fgm, DuaelFr: Views listing page displays too few items on a page
2016-08-29 13:41:31 +00:00
// List views.
2017-03-04 01:20:24 +00:00
'views_ui_views_listing_table' => [
'variables' => [
Issue #2574767 by dipakmdhrm, Manjit.Singh, malavya, Dom., yoroy, xjm, tkoleary, Gábor Hojtsy, dawehner, Cottser, eelkeblok, Bojhan, fgm, DuaelFr: Views listing page displays too few items on a page
2016-08-29 13:41:31 +00:00
'headers' => NULL ,
'rows' => NULL ,
2017-03-04 01:20:24 +00:00
'attributes' => [],
],
Issue #2574767 by dipakmdhrm, Manjit.Singh, malavya, Dom., yoroy, xjm, tkoleary, Gábor Hojtsy, dawehner, Cottser, eelkeblok, Bojhan, fgm, DuaelFr: Views listing page displays too few items on a page
2016-08-29 13:41:31 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
'views_ui_view_displays_list' => [
'variables' => [ 'displays' => []],
],
Issue #2574767 by dipakmdhrm, Manjit.Singh, malavya, Dom., yoroy, xjm, tkoleary, Gábor Hojtsy, dawehner, Cottser, eelkeblok, Bojhan, fgm, DuaelFr: Views listing page displays too few items on a page
2016-08-29 13:41:31 +00:00
2012-08-17 15:09:47 +00:00
// Group of filters.
2017-03-04 01:20:24 +00:00
'views_ui_build_group_filter_form' => [
2012-08-17 15:09:47 +00:00
'render element' => 'form' ,
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
2012-08-17 15:09:47 +00:00
2009-05-17 11:16:51 +00:00
// On behalf of a plugin
2017-03-04 01:20:24 +00:00
'views_ui_style_plugin_table' => [
2009-05-17 11:16:51 +00:00
'render element' => 'form' ,
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
2009-05-17 11:16:51 +00:00
// When previewing a view.
2017-03-04 01:20:24 +00:00
'views_ui_view_preview_section' => [
'variables' => [ 'view' => NULL , 'section' => NULL , 'content' => NULL , 'links' => '' ],
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
2009-05-17 11:16:51 +00:00
// Generic container wrapper, to use instead of theme_container when an id
// is not desired.
2017-03-04 01:20:24 +00:00
'views_ui_container' => [
'variables' => [ 'children' => NULL , 'attributes' => []],
2012-12-11 20:51:50 +00:00
'file' => 'views_ui.theme.inc' ,
2017-03-04 01:20:24 +00:00
],
];
2009-05-17 11:16:51 +00:00
}
/**
2013-10-03 20:55:34 +00:00
* Implements hook_preprocess_HOOK () for views templates .
2009-05-17 11:16:51 +00:00
*/
2013-07-13 10:29:57 +00:00
function views_ui_preprocess_views_view ( & $variables ) {
$view = $variables [ 'view' ];
2014-09-11 16:08:26 +00:00
// Render title for the admin preview.
if ( ! empty ( $view -> live_preview )) {
2017-01-11 21:23:22 +00:00
$variables [ 'title' ] = [
2018-05-11 09:40:33 +00:00
'#markup' => $view -> getTitle (),
2017-01-11 21:23:22 +00:00
];
2014-09-11 16:08:26 +00:00
}
2013-09-16 03:58:06 +00:00
if ( ! empty ( $view -> live_preview ) && \Drupal :: moduleHandler () -> moduleExists ( 'contextual' )) {
2013-04-20 03:41:20 +00:00
$view -> setShowAdminLinks ( FALSE );
2017-03-04 01:20:24 +00:00
foreach ([ 'title' , 'header' , 'exposed' , 'rows' , 'pager' , 'more' , 'footer' , 'empty' , 'attachment_after' , 'attachment_before' ] as $section ) {
2013-07-13 10:29:57 +00:00
if ( ! empty ( $variables [ $section ])) {
2017-03-04 01:20:24 +00:00
$variables [ $section ] = [
2009-05-17 11:16:51 +00:00
'#theme' => 'views_ui_view_preview_section' ,
'#view' => $view ,
'#section' => $section ,
2013-07-13 10:29:57 +00:00
'#content' => $variables [ $section ],
2017-03-04 01:20:24 +00:00
'#theme_wrappers' => [ 'views_ui_container' ],
'#attributes' => [ 'class' => [ 'contextual-region' ]],
];
2009-05-17 11:16:51 +00:00
}
}
}
}
2017-12-05 03:20:01 +00:00
/**
* Implements hook_theme_suggestions_HOOK () .
*/
function views_ui_theme_suggestions_views_ui_view_preview_section ( array $variables ) {
return [ 'views_ui_view_preview_section__' . $variables [ 'section' ]];
}
2009-05-17 11:16:51 +00:00
/**
* Returns contextual links for each handler of a certain section .
*
* @ TODO
* Bring in relationships
* Refactor this function to use much stuff of views_ui_edit_form_get_bucket .
*
* @ param $title
* Add a bolded title of this section .
*/
2012-12-17 22:35:26 +00:00
function views_ui_view_preview_section_handler_links ( ViewExecutable $view , $type , $title = FALSE ) {
$display = $view -> display_handler -> display ;
$handlers = $view -> display_handler -> getHandlers ( $type );
2017-03-04 01:20:24 +00:00
$links = [];
2009-05-17 11:16:51 +00:00
2014-03-25 19:52:54 +00:00
$types = ViewExecutable :: getHandlerTypes ();
2009-05-17 11:16:51 +00:00
if ( $title ) {
2017-03-04 01:20:24 +00:00
$links [ $type . '-title' ] = [
2009-05-17 11:16:51 +00:00
'title' => $types [ $type ][ 'title' ],
2017-03-04 01:20:24 +00:00
];
2009-05-17 11:16:51 +00:00
}
foreach ( $handlers as $id => $handler ) {
2012-09-03 18:44:24 +00:00
$field_name = $handler -> adminLabel ( TRUE );
2017-03-04 01:20:24 +00:00
$links [ $type . '-edit-' . $id ] = [
'title' => t ( 'Edit @section' , [ '@section' => $field_name ]),
2014-10-09 06:39:37 +00:00
'url' => Url :: fromRoute ( 'views_ui.form_handler' , [ 'js' => 'nojs' , 'view' => $view -> storage -> id (), 'display_id' => $display [ 'id' ], 'type' => $type , 'id' => $id ]),
2017-03-04 01:20:24 +00:00
'attributes' => [ 'class' => [ 'views-ajax-link' ]],
];
2009-05-17 11:16:51 +00:00
}
2017-03-04 01:20:24 +00:00
$links [ $type . '-add' ] = [
2009-05-17 11:16:51 +00:00
'title' => t ( 'Add new' ),
2014-10-09 06:39:37 +00:00
'url' => Url :: fromRoute ( 'views_ui.form_add_handler' , [ 'js' => 'nojs' , 'view' => $view -> storage -> id (), 'display_id' => $display [ 'id' ], 'type' => $type ]),
2017-03-04 01:20:24 +00:00
'attributes' => [ 'class' => [ 'views-ajax-link' ]],
];
2009-05-17 11:16:51 +00:00
return $links ;
}
/**
* Returns a link to editing a certain display setting .
*/
2012-12-17 22:35:26 +00:00
function views_ui_view_preview_section_display_category_links ( ViewExecutable $view , $type , $title ) {
2009-05-17 11:16:51 +00:00
$display = $view -> display_handler -> display ;
2017-03-04 01:20:24 +00:00
$links = [
$type . '-edit' => [
'title' => t ( 'Edit @section' , [ '@section' => $title ]),
2014-10-09 06:39:37 +00:00
'url' => Url :: fromRoute ( 'views_ui.form_display' , [ 'js' => 'nojs' , 'view' => $view -> storage -> id (), 'display_id' => $display [ 'id' ], 'type' => $type ]),
2017-03-04 01:20:24 +00:00
'attributes' => [ 'class' => [ 'views-ajax-link' ]],
],
];
2009-05-17 11:16:51 +00:00
return $links ;
}
/**
* Returns all contextual links for the main content part of the view .
*/
2012-12-17 22:35:26 +00:00
function views_ui_view_preview_section_rows_links ( ViewExecutable $view ) {
2017-03-04 01:20:24 +00:00
$links = [];
2009-05-17 11:16:51 +00:00
$links = array_merge ( $links , views_ui_view_preview_section_handler_links ( $view , 'filter' , TRUE ));
$links = array_merge ( $links , views_ui_view_preview_section_handler_links ( $view , 'field' , TRUE ));
$links = array_merge ( $links , views_ui_view_preview_section_handler_links ( $view , 'sort' , TRUE ));
$links = array_merge ( $links , views_ui_view_preview_section_handler_links ( $view , 'argument' , TRUE ));
$links = array_merge ( $links , views_ui_view_preview_section_handler_links ( $view , 'relationship' , TRUE ));
return $links ;
}
/**
2012-10-04 20:27:16 +00:00
* Implements hook_views_plugins_display_alter () .
2009-05-17 11:16:51 +00:00
*/
2012-10-04 20:27:16 +00:00
function views_ui_views_plugins_display_alter ( & $plugins ) {
2009-05-17 11:16:51 +00:00
// Attach contextual links to each display plugin. The links will point to
2013-01-19 05:10:42 +00:00
// paths underneath "admin/structure/views/view/{$view->id()}" (i.e., paths
2009-05-17 11:16:51 +00:00
// for editing and performing other contextual actions on the view).
2012-10-04 20:27:16 +00:00
foreach ( $plugins as & $display ) {
2017-03-04 01:20:24 +00:00
$display [ 'contextual links' ][ 'entity.view.edit_form' ] = [
2014-08-29 06:53:38 +00:00
'route_name' => 'entity.view.edit_form' ,
2017-03-04 01:20:24 +00:00
'route_parameters_names' => [ 'view' => 'id' ],
];
2009-05-17 11:16:51 +00:00
}
}
/**
* Implements hook_contextual_links_view_alter () .
*/
function views_ui_contextual_links_view_alter ( & $element , $items ) {
// Remove contextual links from being rendered, when so desired, such as
// within a View preview.
if ( views_ui_contextual_links_suppress ()) {
2017-03-04 01:20:24 +00:00
$element [ '#links' ] = [];
2009-05-17 11:16:51 +00:00
}
// Append the display ID to the Views UI edit links, so that clicking on the
// contextual link takes you directly to the correct display tab on the edit
// screen.
2014-08-29 06:53:38 +00:00
elseif ( ! empty ( $element [ '#links' ][ 'entityviewedit-form' ])) {
$display_id = $items [ 'entity.view.edit_form' ][ 'metadata' ][ 'display_id' ];
2014-10-09 06:39:37 +00:00
$route_parameters = $element [ '#links' ][ 'entityviewedit-form' ][ 'url' ] -> getRouteParameters () + [ 'display_id' => $display_id ];
$element [ '#links' ][ 'entityviewedit-form' ][ 'url' ] = Url :: fromRoute ( 'entity.view.edit_display_form' , $route_parameters );
2009-05-17 11:16:51 +00:00
}
}
/**
* Sets a static variable for controlling whether contextual links are rendered .
*
* @ see views_ui_contextual_links_view_alter ()
*/
function views_ui_contextual_links_suppress ( $set = NULL ) {
$suppress = & drupal_static ( __FUNCTION__ );
if ( isset ( $set )) {
$suppress = $set ;
}
return $suppress ;
}
/**
* Increments the views_ui_contextual_links_suppress () static variable .
*
* When this function is added to the #pre_render of an element, and
* 'views_ui_contextual_links_suppress_pop' is added to the #post_render of the
* same element , then all contextual links within the element and its
* descendants are suppressed from being rendered . This is used , for example ,
* during a View preview , when it is not desired for nodes in the Views result
* to have contextual links .
*
* @ see views_ui_contextual_links_suppress_pop ()
*/
function views_ui_contextual_links_suppress_push () {
2016-04-29 11:02:20 +00:00
views_ui_contextual_links_suppress ((( int ) views_ui_contextual_links_suppress ()) + 1 );
2009-05-17 11:16:51 +00:00
}
/**
* Decrements the views_ui_contextual_links_suppress () static variable .
*
* @ see views_ui_contextual_links_suppress_push ()
*/
function views_ui_contextual_links_suppress_pop () {
2016-04-29 11:02:20 +00:00
views_ui_contextual_links_suppress ((( int ) views_ui_contextual_links_suppress ()) - 1 );
2009-05-17 11:16:51 +00:00
}
2012-09-26 20:02:06 +00:00
/**
* Implements hook_views_analyze () .
*
* This is the basic views analysis that checks for very minimal problems .
* There are other analysis tools in core specific sections , such as
* node . views . inc as well .
*/
2013-01-10 17:14:16 +00:00
function views_ui_views_analyze ( ViewExecutable $view ) {
2017-03-04 01:20:24 +00:00
$ret = [];
2012-09-26 20:02:06 +00:00
// Check for something other than the default display:
if ( count ( $view -> displayHandlers ) < 2 ) {
$ret [] = Analyzer :: formatMessage ( t ( 'This view has only a default display and therefore will not be placed anywhere on your site; perhaps you want to add a page or a block display.' ), 'warning' );
}
2019-04-17 07:30:13 +00:00
// If a display has a path, check that it does not match an existing path
// alias. This results in the path alias not working.
2012-09-26 20:02:06 +00:00
foreach ( $view -> displayHandlers as $display ) {
if ( empty ( $display )) {
continue ;
}
if ( $display -> hasPath () && $path = $display -> getOption ( 'path' )) {
2019-11-18 14:14:57 +00:00
$normal_path = \Drupal :: service ( 'path_alias.manager' ) -> getPathByAlias ( $path );
2012-09-26 20:02:06 +00:00
if ( $path != $normal_path ) {
2017-03-04 01:20:24 +00:00
$ret [] = Analyzer :: formatMessage ( t ( 'You have configured display %display with a path which is an path alias as well. This might lead to unwanted effects so better use an internal path.' , [ '%display' => $display -> display [ 'display_title' ]]), 'warning' );
2012-09-26 20:02:06 +00:00
}
}
}
return $ret ;
}
2009-05-17 11:16:51 +00:00
/**
2015-06-08 22:40:25 +00:00
* Truncate strings to a set length and provide a '...' if they truncated .
2009-05-17 11:16:51 +00:00
*
* This is often used in the UI to ensure long strings fit .
*/
function views_ui_truncate ( $string , $length ) {
2018-05-08 16:23:11 +00:00
if ( mb_strlen ( $string ) > $length ) {
$string = mb_substr ( $string , 0 , $length );
2009-05-17 11:16:51 +00:00
$string .= '...' ;
}
return $string ;
}