2007-08-20 07:03:08 +00:00
< ? php
/**
* @ file
2012-11-10 15:25:19 +00:00
* Content administration and module settings user interface .
2007-08-20 07:03:08 +00:00
*/
2013-05-25 20:12:45 +00:00
use Drupal\Core\Language\Language ;
2013-06-11 16:48:35 +00:00
use Drupal\node\NodeInterface ;
2012-11-10 15:25:19 +00:00
2007-08-20 07:03:08 +00:00
/**
2011-11-28 11:44:25 +00:00
* Page callback : Form constructor for the permission rebuild confirmation form .
*
2012-10-04 16:41:38 +00:00
* @ return array
* An array as expected by drupal_render () .
*
2011-11-28 11:44:25 +00:00
* @ see node_configure_rebuild_confirm_submit ()
2012-10-04 16:41:38 +00:00
* @ see node_menu ()
2012-11-10 15:25:19 +00:00
* @ ingroup forms
2007-08-20 07:03:08 +00:00
*/
function node_configure_rebuild_confirm () {
2007-11-12 19:06:33 +00:00
return confirm_form ( array (), t ( 'Are you sure you want to rebuild the permissions on site content?' ),
2009-07-06 13:52:57 +00:00
'admin/reports/status' , t ( 'This action rebuilds all permissions on site content, and may be a lengthy process. This action cannot be undone.' ), t ( 'Rebuild permissions' ), t ( 'Cancel' ));
2007-08-20 07:03:08 +00:00
}
/**
2011-11-28 11:44:25 +00:00
* Form submission handler for node_configure_rebuild_confirm () .
2007-08-20 07:03:08 +00:00
*/
2007-08-29 19:56:09 +00:00
function node_configure_rebuild_confirm_submit ( $form , & $form_state ) {
2007-09-02 14:42:30 +00:00
node_access_rebuild ( TRUE );
2009-06-05 05:28:28 +00:00
$form_state [ 'redirect' ] = 'admin/reports/status' ;
2007-08-20 07:03:08 +00:00
}
2007-12-20 09:08:35 +00:00
/**
2011-11-28 11:44:25 +00:00
* Updates all nodes in the passed - in array with the passed - in field values .
2007-12-20 09:08:35 +00:00
*
2012-11-10 15:25:19 +00:00
* IMPORTANT NOTE : This function is intended to work when called from a form
* submission handler . Calling it outside of the form submission process may not
* work correctly .
2007-12-20 09:08:35 +00:00
*
* @ param array $nodes
2013-06-11 16:48:35 +00:00
* Array of node nids or nodes to update .
2007-12-20 09:08:35 +00:00
* @ param array $updates
2012-11-10 15:25:19 +00:00
* Array of key / value pairs with node field names and the value to update that
* field to .
2013-05-26 20:18:10 +00:00
* @ param string $langcode
* ( optional ) The language updates should be applied to . If none is specified
* all available languages are processed .
2013-06-11 16:48:35 +00:00
* @ param bool $load
* ( optional ) TRUE if $nodes contains an array of node IDs to be loaded , FALSE
* if it contains fully loaded nodes . Defaults to FALSE .
2007-12-20 09:08:35 +00:00
*/
2013-06-11 16:48:35 +00:00
function node_mass_update ( array $nodes , array $updates , $langcode = NULL , $load = FALSE ) {
2007-12-20 09:08:35 +00:00
// We use batch processing to prevent timeout when updating a large number
// of nodes.
if ( count ( $nodes ) > 10 ) {
$batch = array (
'operations' => array (
2013-06-11 16:48:35 +00:00
array ( '_node_mass_update_batch_process' , array ( $nodes , $updates , $langcode , $load ))
2007-12-20 09:08:35 +00:00
),
'finished' => '_node_mass_update_batch_finished' ,
'title' => t ( 'Processing' ),
// We use a single multi-pass operation, so the default
// 'Remaining x of y operations' message will be confusing here.
'progress_message' => '' ,
'error_message' => t ( 'The update has encountered an error.' ),
// The operations do not live in the .module file, so we need to
// tell the batch engine which file to load before calling them.
2008-04-14 17:48:46 +00:00
'file' => drupal_get_path ( 'module' , 'node' ) . '/node.admin.inc' ,
2007-12-20 09:08:35 +00:00
);
batch_set ( $batch );
}
else {
2013-06-11 16:48:35 +00:00
if ( $load ) {
$nodes = entity_load_multiple ( 'node' , $nodes );
}
foreach ( $nodes as $node ) {
_node_mass_update_helper ( $node , $updates , $langcode );
2007-12-20 09:08:35 +00:00
}
drupal_set_message ( t ( 'The update has been performed.' ));
}
}
/**
2011-11-28 11:44:25 +00:00
* Updates individual nodes when fewer than 10 are queued .
*
2013-06-11 16:48:35 +00:00
* @ param \Drupal\node\NodeInterface $node
* A node to update .
* @ param array $updates
2011-11-28 11:44:25 +00:00
* Associative array of updates .
2013-05-26 20:18:10 +00:00
* @ param string $langcode
* ( optional ) The language updates should be applied to . If none is specified
* all available languages are processed .
2011-11-28 11:44:25 +00:00
*
2013-06-11 16:48:35 +00:00
* @ return \Drupal\node\NodeInterface
2012-10-04 16:41:38 +00:00
* An updated node object .
*
2011-11-28 11:44:25 +00:00
* @ see node_mass_update ()
2007-12-20 09:08:35 +00:00
*/
2013-06-11 16:48:35 +00:00
function _node_mass_update_helper ( NodeInterface $node , array $updates , $langcode = NULL ) {
2013-05-26 20:18:10 +00:00
$langcodes = isset ( $langcode ) ? array ( $langcode ) : array_keys ( $node -> getTranslationLanguages ());
2010-11-30 19:31:47 +00:00
// For efficiency manually save the original node before applying any changes.
$node -> original = clone $node ;
2013-05-26 20:18:10 +00:00
foreach ( $langcodes as $langcode ) {
foreach ( $updates as $name => $value ) {
$node -> getTranslation ( $langcode , FALSE ) -> $name = $value ;
}
2007-12-20 09:08:35 +00:00
}
2012-04-26 16:44:37 +00:00
$node -> save ();
2007-12-20 09:08:35 +00:00
return $node ;
}
/**
2011-11-28 11:44:25 +00:00
* Executes a batch operation for node_mass_update () .
2012-10-04 16:41:38 +00:00
*
* @ param array $nodes
* An array of node IDs .
* @ param array $updates
* Associative array of updates .
2013-06-11 16:48:35 +00:00
* @ param bool $load
* TRUE if $nodes contains an array of node IDs to be loaded , FALSE if it
* contains fully loaded nodes .
2012-10-04 16:41:38 +00:00
* @ param array $context
* An array of contextual key / values .
2007-12-20 09:08:35 +00:00
*/
2013-06-11 16:48:35 +00:00
function _node_mass_update_batch_process ( array $nodes , array $updates , $load , array & $context ) {
2007-12-20 09:08:35 +00:00
if ( ! isset ( $context [ 'sandbox' ][ 'progress' ])) {
$context [ 'sandbox' ][ 'progress' ] = 0 ;
$context [ 'sandbox' ][ 'max' ] = count ( $nodes );
$context [ 'sandbox' ][ 'nodes' ] = $nodes ;
}
// Process nodes by groups of 5.
$count = min ( 5 , count ( $context [ 'sandbox' ][ 'nodes' ]));
for ( $i = 1 ; $i <= $count ; $i ++ ) {
// For each nid, load the node, reset the values, and save it.
2013-06-11 16:48:35 +00:00
$node = array_shift ( $context [ 'sandbox' ][ 'nodes' ]);
if ( $load ) {
$node = entity_load ( 'node' , $node );
}
$node = _node_mass_update_helper ( $node , $updates );
2007-12-20 09:08:35 +00:00
// Store result for post-processing in the finished callback.
2012-07-22 18:07:00 +00:00
$context [ 'results' ][] = l ( $node -> label (), 'node/' . $node -> nid );
2007-12-20 09:08:35 +00:00
// Update our progress information.
$context [ 'sandbox' ][ 'progress' ] ++ ;
}
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ( $context [ 'sandbox' ][ 'progress' ] != $context [ 'sandbox' ][ 'max' ]) {
$context [ 'finished' ] = $context [ 'sandbox' ][ 'progress' ] / $context [ 'sandbox' ][ 'max' ];
}
}
/**
2011-11-28 11:44:25 +00:00
* Reports the 'finished' status of batch operation for node_mass_update () .
2012-10-04 16:41:38 +00:00
*
* @ param bool $success
* A boolean indicating whether the batch mass update operation successfully
* concluded .
* @ param int $results
* The number of nodes updated via the batch mode process .
* @ param array $operations
* An array of function calls ( not used in this function ) .
2007-12-20 09:08:35 +00:00
*/
function _node_mass_update_batch_finished ( $success , $results , $operations ) {
if ( $success ) {
drupal_set_message ( t ( 'The update has been performed.' ));
}
else {
drupal_set_message ( t ( 'An error occurred and processing did not complete.' ), 'error' );
$message = format_plural ( count ( $results ), '1 item successfully processed:' , '@count items successfully processed:' );
2013-06-10 10:12:28 +00:00
$item_list = array (
'#theme' => 'item_list' ,
'#items' => $results ,
);
$message .= drupal_render ( $item_list );
2007-12-20 09:08:35 +00:00
drupal_set_message ( $message );
}
}
2008-01-29 11:08:17 +00:00
/**
2011-11-28 11:44:25 +00:00
* Returns the admin form object to node_admin_content () .
*
* @ ingroup forms
2008-01-29 11:08:17 +00:00
*/
2007-08-20 07:03:08 +00:00
function node_admin_nodes () {
2012-03-19 06:57:15 +00:00
// Enable language column and filter if multiple languages are enabled.
$multilingual = language_multilingual ();
2009-11-14 07:58:50 +00:00
// Build the sortable table header.
$header = array (
2012-09-26 18:26:15 +00:00
'title' => array (
'data' => t ( 'Title' ),
'field' => 'n.title' ,
),
'type' => array (
'data' => t ( 'Content type' ),
'field' => 'n.type' ,
'class' => array ( RESPONSIVE_PRIORITY_MEDIUM ),
),
'author' => array (
'data' => t ( 'Author' ),
'class' => array ( RESPONSIVE_PRIORITY_LOW ),
),
'status' => array (
'data' => t ( 'Status' ),
'field' => 'n.status' ,
),
'changed' => array (
'data' => t ( 'Updated' ),
'field' => 'n.changed' ,
'sort' => 'desc' ,
'class' => array ( RESPONSIVE_PRIORITY_LOW )
,)
2009-11-14 07:58:50 +00:00
);
2012-03-19 06:57:15 +00:00
if ( $multilingual ) {
2012-09-26 18:26:15 +00:00
$header [ 'language_name' ] = array ( 'data' => t ( 'Language' ), 'field' => 'n.langcode' , 'class' => array ( RESPONSIVE_PRIORITY_LOW ));
2009-11-14 07:58:50 +00:00
}
$header [ 'operations' ] = array ( 'data' => t ( 'Operations' ));
2013-05-26 20:18:10 +00:00
$query = db_select ( 'node_field_data' , 'n' )
2012-06-05 05:42:19 +00:00
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
2012-06-08 12:26:56 +00:00
-> extend ( 'Drupal\Core\Database\Query\TableSortExtender' );
2009-11-14 07:58:50 +00:00
if ( ! user_access ( 'bypass node access' )) {
// If the user is able to view their own unpublished nodes, allow them
// to see these in addition to published nodes. Check that they actually
// have some unpublished nodes to view before adding the condition.
2013-05-26 20:18:10 +00:00
if ( user_access ( 'view own unpublished content' ) && $own_unpublished = db_query ( 'SELECT DISTINCT nid FROM {node_field_data} WHERE uid = :uid AND status = :status' , array ( ':uid' => $GLOBALS [ 'user' ] -> uid , ':status' => 0 )) -> fetchCol ()) {
2009-11-14 07:58:50 +00:00
$query -> condition ( db_or ()
-> condition ( 'n.status' , 1 )
-> condition ( 'n.nid' , $own_unpublished , 'IN' )
);
}
else {
// If not, restrict the query to published nodes.
$query -> condition ( 'n.status' , 1 );
}
}
$nids = $query
2013-05-26 20:18:10 +00:00
-> distinct ()
-> fields ( 'n' , array ( 'nid' ))
2009-11-14 07:58:50 +00:00
-> limit ( 50 )
-> orderByHeader ( $header )
2012-08-19 14:54:14 +00:00
-> addTag ( 'node_access' )
2009-11-14 07:58:50 +00:00
-> execute ()
-> fetchCol ();
$nodes = node_load_multiple ( $nids );
// Prepare the list of nodes.
2013-05-25 20:12:45 +00:00
$languages = language_list ( Language :: STATE_ALL );
2007-08-20 07:03:08 +00:00
$destination = drupal_get_destination ();
2012-12-26 18:15:32 +00:00
$form [ 'nodes' ] = array (
'#type' => 'table' ,
'#header' => $header ,
'#empty' => t ( 'No content available.' ),
);
2009-11-14 07:58:50 +00:00
foreach ( $nodes as $node ) {
2013-05-25 20:12:45 +00:00
$l_options = $node -> langcode != Language :: LANGCODE_NOT_SPECIFIED && isset ( $languages [ $node -> langcode ]) ? array ( 'language' => $languages [ $node -> langcode ]) : array ();
2013-06-10 10:12:28 +00:00
$mark = array (
'#theme' => 'mark' ,
2013-06-11 16:35:45 +00:00
'#mark_type' => node_mark ( $node -> nid , $node -> changed ),
2013-06-10 10:12:28 +00:00
);
2012-12-26 18:15:32 +00:00
$form [ 'nodes' ][ $node -> nid ][ 'title' ] = array (
'#type' => 'link' ,
'#title' => $node -> label (),
'#href' => 'node/' . $node -> nid ,
'#options' => $l_options ,
2013-06-10 10:12:28 +00:00
'#suffix' => ' ' . drupal_render ( $mark ),
2012-12-26 18:15:32 +00:00
);
$form [ 'nodes' ][ $node -> nid ][ 'type' ] = array (
'#markup' => check_plain ( node_get_type_label ( $node )),
);
$form [ 'nodes' ][ $node -> nid ][ 'author' ] = array (
'#theme' => 'username' ,
'#account' => $node ,
);
$form [ 'nodes' ][ $node -> nid ][ 'status' ] = array (
'#markup' => $node -> status ? t ( 'published' ) : t ( 'not published' ),
);
$form [ 'nodes' ][ $node -> nid ][ 'changed' ] = array (
'#markup' => format_date ( $node -> changed , 'short' ),
2009-10-08 18:26:33 +00:00
);
2012-03-19 06:57:15 +00:00
if ( $multilingual ) {
2012-12-26 18:15:32 +00:00
$form [ 'nodes' ][ $node -> nid ][ 'language_name' ] = array (
'#markup' => language_name ( $node -> langcode ),
);
2007-08-20 07:03:08 +00:00
}
2012-12-26 18:15:32 +00:00
2009-11-14 07:58:50 +00:00
// Build a list of all the accessible operations for the current node.
$operations = array ();
if ( node_access ( 'update' , $node )) {
$operations [ 'edit' ] = array (
2013-02-15 02:53:23 +00:00
'title' => t ( 'Edit' ),
2009-11-14 07:58:50 +00:00
'href' => 'node/' . $node -> nid . '/edit' ,
'query' => $destination ,
);
}
if ( node_access ( 'delete' , $node )) {
$operations [ 'delete' ] = array (
2013-02-15 02:53:23 +00:00
'title' => t ( 'Delete' ),
2009-11-14 07:58:50 +00:00
'href' => 'node/' . $node -> nid . '/delete' ,
'query' => $destination ,
);
}
2013-05-26 20:18:10 +00:00
if ( $node -> isTranslatable ()) {
2012-11-22 16:03:57 +00:00
$operations [ 'translate' ] = array (
2013-02-15 02:53:23 +00:00
'title' => t ( 'Translate' ),
2012-11-22 16:03:57 +00:00
'href' => 'node/' . $node -> nid . '/translations' ,
'query' => $destination ,
);
}
2012-12-26 18:15:32 +00:00
$form [ 'nodes' ][ $node -> nid ][ 'operations' ] = array ();
2009-11-14 07:58:50 +00:00
if ( count ( $operations ) > 1 ) {
// Render an unordered list of operations links.
2012-12-26 18:15:32 +00:00
$form [ 'nodes' ][ $node -> nid ][ 'operations' ] = array (
'#type' => 'operations' ,
'#subtype' => 'node' ,
'#links' => $operations ,
2009-11-14 07:58:50 +00:00
);
}
elseif ( ! empty ( $operations )) {
// Render the first and only operation as a link.
$link = reset ( $operations );
2012-12-26 18:15:32 +00:00
$form [ 'nodes' ][ $node -> nid ][ 'operations' ] = array (
'#type' => 'link' ,
'#title' => $link [ 'title' ],
'#href' => $link [ 'href' ],
'#options' => array ( 'query' => $link [ 'query' ]),
2009-11-14 07:58:50 +00:00
);
}
}
2012-12-26 18:15:32 +00:00
$form [ 'pager' ] = array ( '#theme' => 'pager' );
2007-08-20 07:03:08 +00:00
return $form ;
}