Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
< ? php
/**
* @ file
* Provides full - site preview functionality for content staging .
*/
use Drupal\Component\Serialization\Json ;
2023-07-10 22:24:59 +00:00
use Drupal\Core\Cache\Cache ;
2018-07-16 17:03:55 +00:00
use Drupal\Core\Entity\EntityFormInterface ;
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
use Drupal\Core\Entity\EntityInterface ;
2019-09-20 10:17:19 +00:00
use Drupal\Core\Entity\EntityTypeInterface ;
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
use Drupal\Core\Form\FormStateInterface ;
use Drupal\Core\Routing\RouteMatchInterface ;
use Drupal\Core\Session\AccountInterface ;
2019-08-30 11:59:59 +00:00
use Drupal\Core\Url ;
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
use Drupal\views\Plugin\views\query\QueryPluginBase ;
use Drupal\views\ViewExecutable ;
2018-07-24 11:36:16 +00:00
use Drupal\workspaces\EntityAccess ;
use Drupal\workspaces\EntityOperations ;
use Drupal\workspaces\EntityTypeInfo ;
use Drupal\workspaces\FormOperations ;
use Drupal\workspaces\ViewsQueryAlter ;
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
/**
* Implements hook_help () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_help ( $route_name , RouteMatchInterface $route_match ) {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
switch ( $route_name ) {
2018-07-24 11:36:16 +00:00
// Main module help for the Workspaces module.
case 'help.page.workspaces' :
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
$output = '' ;
$output .= '<h3>' . t ( 'About' ) . '</h3>' ;
2022-09-26 16:14:12 +00:00
$output .= '<p>' . t ( 'The Workspaces module allows workspaces to be defined and switched between. Content is then assigned to the active workspace when created. For more information, see the <a href=":workspaces">online documentation for the Workspaces module</a>.' , [ ':workspaces' => 'https://www.drupal.org/docs/8/core/modules/workspace/overview' ]) . '</p>' ;
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return $output ;
}
}
/**
* Implements hook_entity_type_build () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_entity_type_build ( array & $entity_types ) {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityTypeInfo :: class )
-> entityTypeBuild ( $entity_types );
}
2019-10-07 22:44:04 +00:00
/**
* Implements hook_entity_type_alter () .
*/
function workspaces_entity_type_alter ( array & $entity_types ) {
\Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityTypeInfo :: class )
-> entityTypeAlter ( $entity_types );
}
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
/**
* Implements hook_form_alter () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_form_alter ( & $form , FormStateInterface $form_state , $form_id ) {
2018-07-16 17:03:55 +00:00
if ( $form_state -> getFormObject () instanceof EntityFormInterface ) {
\Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityOperations :: class )
-> entityFormAlter ( $form , $form_state , $form_id );
}
\Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( FormOperations :: class )
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
-> formAlter ( $form , $form_state , $form_id );
}
2018-11-13 17:49:25 +00:00
/**
* Implements hook_field_info_alter () .
*/
function workspaces_field_info_alter ( & $definitions ) {
\Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityTypeInfo :: class )
-> fieldInfoAlter ( $definitions );
}
2019-09-20 10:17:19 +00:00
/**
* Implements hook_entity_base_field_info () .
*/
function workspaces_entity_base_field_info ( EntityTypeInterface $entity_type ) {
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityTypeInfo :: class )
-> entityBaseFieldInfo ( $entity_type );
}
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
/**
2018-11-20 13:49:20 +00:00
* Implements hook_entity_preload () .
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
*/
2018-11-20 13:49:20 +00:00
function workspaces_entity_preload ( array $ids , $entity_type_id ) {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityOperations :: class )
2018-11-20 13:49:20 +00:00
-> entityPreload ( $ids , $entity_type_id );
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
}
/**
* Implements hook_entity_presave () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_entity_presave ( EntityInterface $entity ) {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityOperations :: class )
-> entityPresave ( $entity );
}
/**
* Implements hook_entity_insert () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_entity_insert ( EntityInterface $entity ) {
2019-11-04 23:30:19 +00:00
if ( $entity -> getEntityTypeId () === 'workspace' ) {
\Drupal :: service ( 'workspaces.association' ) -> workspaceInsert ( $entity );
\Drupal :: service ( 'workspaces.repository' ) -> resetCache ();
}
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityOperations :: class )
-> entityInsert ( $entity );
}
/**
* Implements hook_entity_update () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_entity_update ( EntityInterface $entity ) {
2019-11-04 23:30:19 +00:00
if ( $entity -> getEntityTypeId () === 'workspace' ) {
\Drupal :: service ( 'workspaces.repository' ) -> resetCache ();
}
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityOperations :: class )
-> entityUpdate ( $entity );
}
2018-07-16 17:03:55 +00:00
/**
* Implements hook_entity_predelete () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_entity_predelete ( EntityInterface $entity ) {
2019-11-04 23:30:19 +00:00
if ( $entity -> getEntityTypeId () === 'workspace' ) {
\Drupal :: service ( 'workspaces.repository' ) -> resetCache ();
}
2018-07-16 17:03:55 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityOperations :: class )
-> entityPredelete ( $entity );
}
2023-01-13 09:49:25 +00:00
/**
* Implements hook_entity_delete () .
*/
function workspaces_entity_delete ( EntityInterface $entity ) {
if ( \Drupal :: service ( 'workspaces.manager' ) -> isEntityTypeSupported ( $entity -> getEntityType ())) {
\Drupal :: service ( 'workspaces.association' )
-> deleteAssociations ( NULL , $entity -> getEntityTypeId (), [ $entity -> id ()]);
}
}
/**
* Implements hook_entity_revision_delete () .
*/
function workspaces_entity_revision_delete ( EntityInterface $entity ) {
if ( \Drupal :: service ( 'workspaces.manager' ) -> isEntityTypeSupported ( $entity -> getEntityType ())) {
\Drupal :: service ( 'workspaces.association' )
-> deleteAssociations ( NULL , $entity -> getEntityTypeId (), [ $entity -> id ()], [ $entity -> getRevisionId ()]);
}
}
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
/**
* Implements hook_entity_access () .
*
2018-07-24 11:36:16 +00:00
* @ see \Drupal\workspaces\EntityAccess
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
*/
2018-07-24 11:36:16 +00:00
function workspaces_entity_access ( EntityInterface $entity , $operation , AccountInterface $account ) {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityAccess :: class )
-> entityOperationAccess ( $entity , $operation , $account );
}
/**
* Implements hook_entity_create_access () .
*
2018-07-24 11:36:16 +00:00
* @ see \Drupal\workspaces\EntityAccess
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
*/
2018-07-24 11:36:16 +00:00
function workspaces_entity_create_access ( AccountInterface $account , array $context , $entity_bundle ) {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( EntityAccess :: class )
-> entityCreateAccess ( $account , $context , $entity_bundle );
}
2023-07-10 22:24:59 +00:00
/**
* Implements hook_ENTITY_TYPE_update () for the 'menu_link_content' entity type .
*/
function workspaces_menu_link_content_update ( EntityInterface $entity ) {
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
if ( $entity -> getLoadedRevisionId () != $entity -> getRevisionId ()) {
// We are not updating the menu tree definitions when a custom menu link
// entity is saved as a pending revision (because the parent can not be
// changed), so we need to clear the system menu cache manually. However,
// inserting or deleting a custom menu link updates the menu tree
// definitions, so we don't have to do anything in those cases.
$cache_tags = Cache :: buildTags ( 'config:system.menu' , [ $entity -> getMenuName ()], '.' );
\Drupal :: service ( 'cache_tags.invalidator' ) -> invalidateTags ( $cache_tags );
}
}
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
/**
* Implements hook_views_query_alter () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_views_query_alter ( ViewExecutable $view , QueryPluginBase $query ) {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return \Drupal :: service ( 'class_resolver' )
-> getInstanceFromDefinition ( ViewsQueryAlter :: class )
-> alterQuery ( $view , $query );
}
/**
* Implements hook_cron () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_cron () {
\Drupal :: service ( 'workspaces.manager' ) -> purgeDeletedWorkspacesBatch ();
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
}
/**
* Implements hook_toolbar () .
*/
2018-07-24 11:36:16 +00:00
function workspaces_toolbar () {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
$items [ 'workspace' ] = [
'#cache' => [
'contexts' => [
'user.permissions' ,
],
],
];
$current_user = \Drupal :: currentUser ();
if ( ! $current_user -> hasPermission ( 'administer workspaces' )
2018-11-15 14:46:35 +00:00
&& ! $current_user -> hasPermission ( 'view own workspace' )
&& ! $current_user -> hasPermission ( 'view any workspace' )) {
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
return $items ;
}
2018-07-24 11:36:16 +00:00
/** @var \Drupal\workspaces\WorkspaceInterface $active_workspace */
$active_workspace = \Drupal :: service ( 'workspaces.manager' ) -> getActiveWorkspace ();
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
2018-11-15 14:46:35 +00:00
$items [ 'workspace' ] += [
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
'#type' => 'toolbar_item' ,
'tab' => [
'#type' => 'link' ,
2019-08-30 11:59:59 +00:00
'#title' => $active_workspace ? $active_workspace -> label () : t ( 'Live' ),
'#url' => Url :: fromRoute ( 'entity.workspace.collection' , [], [ 'query' => \Drupal :: destination () -> getAsArray ()]),
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
'#attributes' => [
'title' => t ( 'Switch workspace' ),
2018-07-16 22:06:11 +00:00
'class' => [ 'use-ajax' , 'toolbar-icon' , 'toolbar-icon-workspace' ],
'data-dialog-type' => 'dialog' ,
'data-dialog-renderer' => 'off_canvas_top' ,
'data-dialog-options' => Json :: encode ([
'height' => 161 ,
'classes' => [
2018-07-24 11:36:16 +00:00
'ui-dialog' => 'workspaces-dialog' ,
2018-07-16 22:06:11 +00:00
],
]),
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
],
2019-08-30 11:59:59 +00:00
'#cache' => [ 'tags' => $active_workspace ? $active_workspace -> getCacheTags () : []],
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
],
'#wrapper_attributes' => [
2018-07-24 11:36:16 +00:00
'class' => [ 'workspaces-toolbar-tab' ],
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
],
'#attached' => [
2018-07-24 11:36:16 +00:00
'library' => [ 'workspaces/drupal.workspaces.toolbar' ],
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
],
'#weight' => 500 ,
];
2019-08-30 11:59:59 +00:00
// Add a special class to the wrapper if we don't have an active workspace so
// we can highlight it with a different color.
if ( ! $active_workspace ) {
2018-07-24 11:36:16 +00:00
$items [ 'workspace' ][ '#wrapper_attributes' ][ 'class' ][] = 'workspaces-toolbar-tab--is-default' ;
Issue #2784921 by amateescu, timmillwood, pk188, plach, catch, Fabianx, dixon_, dawehner, borisson_, Sam152, yoroy, webchick, phenaproxima, larowlan: Add Workspaces experimental module
2018-05-04 12:13:32 +00:00
}
return $items ;
}