2007-08-20 07:03:08 +00:00
< ? php
/**
* @ file
2011-11-28 11:44:25 +00:00
* Callbacks for adding , editing , and deleting content and managing revisions .
*
* Also includes validation , submission and other helper functions .
*
* @ see node_menu ()
2007-08-20 07:03:08 +00:00
*/
/**
2011-11-28 11:44:25 +00:00
* Page callback : Presents the node editing form .
*
* @ see node_menu ()
2007-08-20 07:03:08 +00:00
*/
2009-11-08 10:02:41 +00:00
function node_page_edit ( $node ) {
2009-06-04 03:33:29 +00:00
$type_name = node_type_get_name ( $node );
2010-01-09 21:54:01 +00:00
drupal_set_title ( t ( '<em>Edit @type</em> @title' , array ( '@type' => $type_name , '@title' => $node -> title )), PASS_THROUGH );
2008-04-14 17:48:46 +00:00
return drupal_get_form ( $node -> type . '_node_form' , $node );
2007-08-20 07:03:08 +00:00
}
2011-11-28 11:44:25 +00:00
/**
* Page callback : Presents the node add form .
*
* @ see node_menu ()
*/
2007-08-20 07:03:08 +00:00
function node_add_page () {
$item = menu_get_item ();
2007-12-22 23:24:26 +00:00
$content = system_admin_menu_block ( $item );
2008-08-14 13:47:35 +00:00
// Bypass the node/add listing if only one content type is available.
if ( count ( $content ) == 1 ) {
$item = array_shift ( $content );
drupal_goto ( $item [ 'href' ]);
}
2009-10-09 01:00:08 +00:00
return theme ( 'node_add_list' , array ( 'content' => $content ));
2007-08-20 07:03:08 +00:00
}
2007-12-06 09:58:34 +00:00
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a list of available node types for node creation .
*
* @ param $variables
* An associative array containing :
* - content : An array of content types .
2007-12-06 09:58:34 +00:00
*
2011-11-28 11:44:25 +00:00
* @ see node_add_page ()
2007-12-06 09:58:34 +00:00
* @ ingroup themeable
*/
2009-10-09 01:00:08 +00:00
function theme_node_add_list ( $variables ) {
$content = $variables [ 'content' ];
2007-08-20 07:03:08 +00:00
$output = '' ;
if ( $content ) {
$output = '<dl class="node-type-list">' ;
foreach ( $content as $item ) {
2008-07-10 11:12:02 +00:00
$output .= '<dt>' . l ( $item [ 'title' ], $item [ 'href' ], $item [ 'localized_options' ]) . '</dt>' ;
2008-04-14 17:48:46 +00:00
$output .= '<dd>' . filter_xss_admin ( $item [ 'description' ]) . '</dd>' ;
2007-08-20 07:03:08 +00:00
}
$output .= '</dl>' ;
}
2009-08-04 06:44:48 +00:00
else {
2010-01-09 23:03:22 +00:00
$output = '<p>' . t ( 'You have not created any content types yet. Go to the <a href="@create-content">content type creation page</a> to add a new content type.' , array ( '@create-content' => url ( 'admin/structure/types/add' ))) . '</p>' ;
2009-08-04 06:44:48 +00:00
}
2007-08-20 07:03:08 +00:00
return $output ;
}
/**
2011-11-28 11:44:25 +00:00
* Page callback : Provides the node submission form .
*
* @ param $type
* The node type for the submitted node .
*
* @ return
* Returns a node submission form .
*
* @ see node_menu ()
2007-08-20 07:03:08 +00:00
*/
function node_add ( $type ) {
global $user ;
2009-06-04 03:33:29 +00:00
$types = node_type_get_types ();
2012-03-08 15:10:59 +00:00
$node = ( object ) array ( 'uid' => $user -> uid , 'name' => ( isset ( $user -> name ) ? $user -> name : '' ), 'type' => $type , 'langcode' => LANGUAGE_NOT_SPECIFIED );
2010-11-26 19:23:01 +00:00
drupal_set_title ( t ( 'Create @name' , array ( '@name' => $types [ $type ] -> name )), PASS_THROUGH );
$output = drupal_get_form ( $type . '_node_form' , $node );
2007-08-20 07:03:08 +00:00
return $output ;
}
2011-11-28 11:44:25 +00:00
/**
* Form validation handler for node_form () .
*
* @ see node_form_delete_submit ()
* @ see node_form_build_preview ()
* @ see node_form_submit ()
* @ see node_form_submit_build_node ()
*/
2007-08-20 07:03:08 +00:00
function node_form_validate ( $form , & $form_state ) {
2010-06-17 13:44:45 +00:00
// $form_state['node'] contains the actual entity being edited, but we must
// not update it with form values that have not yet been validated, so we
// create a pseudo-entity to use during validation.
2010-05-06 05:59:31 +00:00
$node = ( object ) $form_state [ 'values' ];
2010-09-03 19:56:51 +00:00
node_validate ( $node , $form , $form_state );
2010-06-17 13:44:45 +00:00
entity_form_field_validate ( 'node' , $form , $form_state );
2007-08-20 07:03:08 +00:00
}
/**
2011-11-28 11:44:25 +00:00
* Form constructor for the node add / edit form .
*
* @ see node_form_delete_submit ()
* @ see node_form_build_preview ()
* @ see node_form_validate ()
* @ see node_form_submit ()
* @ see node_form_submit_build_node ()
* @ ingroup forms
2007-08-20 07:03:08 +00:00
*/
2009-11-08 10:02:41 +00:00
function node_form ( $form , & $form_state , $node ) {
2007-08-20 07:03:08 +00:00
global $user ;
2010-06-17 13:44:45 +00:00
// During initial form build, add the node entity to the form state for use
// during form building and processing. During a rebuild, use what is in the
// form state.
if ( ! isset ( $form_state [ 'node' ])) {
if ( ! isset ( $node -> title )) {
$node -> title = NULL ;
2007-08-20 07:03:08 +00:00
}
2007-09-09 19:52:29 +00:00
node_object_prepare ( $node );
2010-06-17 13:44:45 +00:00
$form_state [ 'node' ] = $node ;
2007-09-09 19:52:29 +00:00
}
2007-10-05 13:14:04 +00:00
else {
2010-06-17 13:44:45 +00:00
$node = $form_state [ 'node' ];
}
// Some special stuff when previewing a node.
if ( isset ( $form_state [ 'node_preview' ])) {
$form [ '#prefix' ] = $form_state [ 'node_preview' ];
2009-06-22 09:10:07 +00:00
$node -> in_preview = TRUE ;
2007-10-05 13:14:04 +00:00
}
2010-06-17 13:44:45 +00:00
else {
unset ( $node -> in_preview );
}
2007-08-20 07:03:08 +00:00
2011-10-21 03:58:07 +00:00
// Override the default CSS class name, since the user-defined node type name
// in 'TYPE-node-form' potentially clashes with third-party class names.
$form [ '#attributes' ][ 'class' ][ 0 ] = drupal_html_class ( 'node-' . $node -> type . '-form' );
2007-08-20 07:03:08 +00:00
2007-12-31 08:54:37 +00:00
// Basic node information.
// These elements are just values so they are not even sent to the client.
2012-02-23 14:22:18 +00:00
foreach ( array ( 'nid' , 'vid' , 'uid' , 'created' , 'type' ) as $key ) {
2007-11-27 13:31:04 +00:00
$form [ $key ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'value' ,
2007-11-27 13:31:04 +00:00
'#value' => isset ( $node -> $key ) ? $node -> $key : NULL ,
);
2007-08-20 07:03:08 +00:00
}
// Changed must be sent to the client, for later overwrite error checking.
2007-11-27 13:31:04 +00:00
$form [ 'changed' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'hidden' ,
2007-11-27 13:31:04 +00:00
'#default_value' => isset ( $node -> changed ) ? $node -> changed : NULL ,
);
2010-09-03 19:56:51 +00:00
// Invoke hook_form() to get the node-specific bits. Can't use node_invoke(),
// because hook_form() needs to be able to receive $form_state by reference.
2010-09-09 23:01:48 +00:00
// @todo hook_form() implementations are unable to add #validate or #submit
// handlers to the form buttons below. Remove hook_form() entirely.
2010-09-03 19:56:51 +00:00
$function = node_type_get_base ( $node ) . '_form' ;
if ( function_exists ( $function ) && ( $extra = $function ( $node , $form_state ))) {
2007-08-20 07:03:08 +00:00
$form = array_merge_recursive ( $form , $extra );
}
2010-10-23 16:30:48 +00:00
// If the node type has a title, and the node type form defined no special
// weight for it, we default to a weight of -5 for consistency.
if ( isset ( $form [ 'title' ]) && ! isset ( $form [ 'title' ][ '#weight' ])) {
2010-01-09 21:54:01 +00:00
$form [ 'title' ][ '#weight' ] = - 5 ;
}
2010-09-09 23:01:48 +00:00
// @todo D8: Remove. Modules should access the node using $form_state['node'].
2007-08-20 07:03:08 +00:00
$form [ '#node' ] = $node ;
2012-02-16 15:52:30 +00:00
if ( variable_get ( 'node_type_language_' . $node -> type , 0 ) && module_exists ( 'language' )) {
2012-04-25 23:44:20 +00:00
$languages = language_list ();
2012-02-16 15:52:30 +00:00
$language_options = array ();
foreach ( $languages as $langcode => $language ) {
$language_options [ $langcode ] = $language -> name ;
}
2012-02-23 14:22:18 +00:00
$form [ 'langcode' ] = array (
2012-02-16 15:52:30 +00:00
'#type' => 'select' ,
'#title' => t ( 'Language' ),
2012-02-23 14:22:18 +00:00
'#default_value' => ( isset ( $node -> langcode ) ? $node -> langcode : '' ),
2012-02-16 15:52:30 +00:00
'#options' => $language_options ,
2012-03-08 15:10:59 +00:00
'#empty_value' => LANGUAGE_NOT_SPECIFIED ,
2012-02-16 15:52:30 +00:00
);
}
else {
2012-02-23 14:22:18 +00:00
$form [ 'langcode' ] = array (
2012-02-16 15:52:30 +00:00
'#type' => 'value' ,
// New nodes without multilingual support get the default language, old
// nodes keep their language if language.module is not available.
2012-02-23 14:22:18 +00:00
'#value' => ! isset ( $form [ '#node' ] -> nid ) ? language_default () -> langcode : $node -> langcode ,
2012-02-16 15:52:30 +00:00
);
}
2009-04-11 22:19:46 +00:00
$form [ 'additional_settings' ] = array (
'#type' => 'vertical_tabs' ,
2009-08-19 13:31:14 +00:00
'#weight' => 99 ,
2009-04-11 22:19:46 +00:00
);
2007-08-20 07:03:08 +00:00
// Add a log field if the "Create new revision" option is checked, or if the
// current user has the ability to check that option.
2009-12-28 11:12:55 +00:00
$form [ 'revision_information' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Revision information' ),
'#collapsible' => TRUE ,
// Collapsed by default when "Create new revision" is unchecked
'#collapsed' => ! $node -> revision ,
'#group' => 'additional_settings' ,
2010-11-05 19:47:20 +00:00
'#attributes' => array (
'class' => array ( 'node-form-revision-information' ),
),
2009-12-28 11:12:55 +00:00
'#attached' => array (
'js' => array ( drupal_get_path ( 'module' , 'node' ) . '/node.js' ),
),
'#weight' => 20 ,
'#access' => $node -> revision || user_access ( 'administer nodes' ),
);
$form [ 'revision_information' ][ 'revision' ] = array (
'#type' => 'checkbox' ,
'#title' => t ( 'Create new revision' ),
'#default_value' => $node -> revision ,
2010-03-24 07:46:06 +00:00
'#access' => user_access ( 'administer nodes' ),
);
// Check the revision log checkbox when the log textarea is filled in.
// This must not happen if "Create new revision" is enabled by default, since
// the state would auto-disable the checkbox otherwise.
if ( ! $node -> revision ) {
$form [ 'revision_information' ][ 'revision' ][ '#states' ] = array (
2009-12-28 11:12:55 +00:00
'checked' => array (
'textarea[name="log"]' => array ( 'empty' => FALSE ),
2009-10-16 19:20:34 +00:00
),
2010-03-24 07:46:06 +00:00
);
}
2009-12-28 11:12:55 +00:00
$form [ 'revision_information' ][ 'log' ] = array (
'#type' => 'textarea' ,
'#title' => t ( 'Revision log message' ),
'#rows' => 4 ,
'#default_value' => ! empty ( $node -> log ) ? $node -> log : '' ,
2011-08-02 00:42:01 +00:00
'#description' => t ( 'Briefly describe the changes you have made.' ),
2009-12-28 11:12:55 +00:00
);
2007-08-20 07:03:08 +00:00
// Node author information for administrators
$form [ 'author' ] = array (
'#type' => 'fieldset' ,
'#access' => user_access ( 'administer nodes' ),
'#title' => t ( 'Authoring information' ),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
2009-04-11 22:19:46 +00:00
'#group' => 'additional_settings' ,
2010-11-05 19:47:20 +00:00
'#attributes' => array (
'class' => array ( 'node-form-author' ),
),
2009-09-05 15:05:05 +00:00
'#attached' => array (
2010-05-04 16:03:34 +00:00
'js' => array (
drupal_get_path ( 'module' , 'node' ) . '/node.js' ,
array (
'type' => 'setting' ,
'data' => array ( 'anonymous' => variable_get ( 'anonymous' , t ( 'Anonymous' ))),
),
),
2009-09-05 15:05:05 +00:00
),
2008-08-22 12:38:02 +00:00
'#weight' => 90 ,
2007-08-20 07:03:08 +00:00
);
2007-11-27 13:31:04 +00:00
$form [ 'author' ][ 'name' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Authored by' ),
'#maxlength' => 60 ,
'#autocomplete_path' => 'user/autocomplete' ,
2008-10-11 20:45:26 +00:00
'#default_value' => ! empty ( $node -> name ) ? $node -> name : '' ,
2007-11-28 10:29:21 +00:00
'#weight' => - 1 ,
2007-11-27 13:31:04 +00:00
'#description' => t ( 'Leave blank for %anonymous.' , array ( '%anonymous' => variable_get ( 'anonymous' , t ( 'Anonymous' )))),
);
$form [ 'author' ][ 'date' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Authored on' ),
'#maxlength' => 25 ,
2010-08-20 01:23:43 +00:00
'#description' => t ( 'Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.' , array ( '%time' => ! empty ( $node -> date ) ? date_format ( date_create ( $node -> date ), 'Y-m-d H:i:s O' ) : format_date ( $node -> created , 'custom' , 'Y-m-d H:i:s O' ), '%timezone' => ! empty ( $node -> date ) ? date_format ( date_create ( $node -> date ), 'O' ) : format_date ( $node -> created , 'custom' , 'O' ))),
2010-01-30 02:52:08 +00:00
'#default_value' => ! empty ( $node -> date ) ? $node -> date : '' ,
2007-11-27 13:31:04 +00:00
);
2007-08-20 07:03:08 +00:00
// Node options for administrators
$form [ 'options' ] = array (
'#type' => 'fieldset' ,
'#access' => user_access ( 'administer nodes' ),
'#title' => t ( 'Publishing options' ),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
2009-04-11 22:19:46 +00:00
'#group' => 'additional_settings' ,
2010-11-05 19:47:20 +00:00
'#attributes' => array (
'class' => array ( 'node-form-options' ),
),
2009-09-05 15:05:05 +00:00
'#attached' => array (
'js' => array ( drupal_get_path ( 'module' , 'node' ) . '/node.js' ),
),
2008-08-22 12:38:02 +00:00
'#weight' => 95 ,
2007-08-20 07:03:08 +00:00
);
2007-11-27 13:31:04 +00:00
$form [ 'options' ][ 'status' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'checkbox' ,
'#title' => t ( 'Published' ),
2007-11-27 13:31:04 +00:00
'#default_value' => $node -> status ,
);
$form [ 'options' ][ 'promote' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'checkbox' ,
'#title' => t ( 'Promoted to front page' ),
2007-11-27 13:31:04 +00:00
'#default_value' => $node -> promote ,
);
$form [ 'options' ][ 'sticky' ] = array (
2007-11-28 10:29:21 +00:00
'#type' => 'checkbox' ,
'#title' => t ( 'Sticky at top of lists' ),
2007-11-27 13:31:04 +00:00
'#default_value' => $node -> sticky ,
);
2007-08-20 07:03:08 +00:00
// Add the buttons.
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-01-03 21:01:04 +00:00
$form [ 'actions' ][ 'submit' ] = array (
2007-08-20 07:03:08 +00:00
'#type' => 'submit' ,
2009-10-16 13:20:16 +00:00
'#access' => variable_get ( 'node_preview_' . $node -> type , DRUPAL_OPTIONAL ) != DRUPAL_REQUIRED || ( ! form_get_errors () && isset ( $form_state [ 'node_preview' ])),
2007-11-29 11:54:37 +00:00
'#value' => t ( 'Save' ),
2007-08-20 07:03:08 +00:00
'#weight' => 5 ,
2007-11-29 11:54:37 +00:00
'#submit' => array ( 'node_form_submit' ),
2007-08-20 07:03:08 +00:00
);
2010-01-03 21:01:04 +00:00
$form [ 'actions' ][ 'preview' ] = array (
2009-10-16 13:20:16 +00:00
'#access' => variable_get ( 'node_preview_' . $node -> type , DRUPAL_OPTIONAL ) != DRUPAL_DISABLED ,
2007-08-20 07:03:08 +00:00
'#type' => 'submit' ,
2007-11-29 11:54:37 +00:00
'#value' => t ( 'Preview' ),
2007-08-20 07:03:08 +00:00
'#weight' => 10 ,
2007-11-29 11:54:37 +00:00
'#submit' => array ( 'node_form_build_preview' ),
2007-08-20 07:03:08 +00:00
);
if ( ! empty ( $node -> nid ) && node_access ( 'delete' , $node )) {
2010-01-03 21:01:04 +00:00
$form [ 'actions' ][ 'delete' ] = array (
2007-08-20 07:03:08 +00:00
'#type' => 'submit' ,
'#value' => t ( 'Delete' ),
'#weight' => 15 ,
'#submit' => array ( 'node_form_delete_submit' ),
);
}
2010-09-09 23:01:48 +00:00
// This form uses a button-level #submit handler for the form's main submit
// action. node_form_submit() manually invokes all form-level #submit handlers
// of the form. Without explicitly setting #submit, Form API would auto-detect
// node_form_submit() as submit handler, but that is the button-level #submit
// handler for the 'Save' action. To maintain backwards compatibility, a
// #submit handler is auto-suggested for custom node type modules.
2007-08-20 07:03:08 +00:00
$form [ '#validate' ][] = 'node_form_validate' ;
2010-09-09 23:01:48 +00:00
if ( ! isset ( $form [ '#submit' ]) && function_exists ( $node -> type . '_node_form_submit' )) {
$form [ '#submit' ][] = $node -> type . '_node_form_submit' ;
}
$form += array ( '#submit' => array ());
2009-02-05 03:42:58 +00:00
2012-02-23 14:22:18 +00:00
field_attach_form ( 'node' , $node , $form , $form_state , $node -> langcode );
2007-08-20 07:03:08 +00:00
return $form ;
}
/**
2011-11-28 11:44:25 +00:00
* Form submission handler for the 'Delete' button for node_form () .
*
* @ see node_form_build_preview ()
* @ see node_form_validate ()
* @ see node_form_submit ()
* @ see node_form_submit_build_node ()
2007-08-20 07:03:08 +00:00
*/
function node_form_delete_submit ( $form , & $form_state ) {
2009-09-29 15:31:17 +00:00
$destination = array ();
2009-09-21 06:44:14 +00:00
if ( isset ( $_GET [ 'destination' ])) {
2007-08-20 07:03:08 +00:00
$destination = drupal_get_destination ();
2009-09-21 06:44:14 +00:00
unset ( $_GET [ 'destination' ]);
2007-08-20 07:03:08 +00:00
}
$node = $form [ '#node' ];
2009-10-15 16:18:46 +00:00
$form_state [ 'redirect' ] = array ( 'node/' . $node -> nid . '/delete' , array ( 'query' => $destination ));
2007-08-20 07:03:08 +00:00
}
2011-11-28 11:44:25 +00:00
/**
* Form submission handler for the 'Preview' button for node_form () .
*
* @ see node_form_delete_submit ()
* @ see node_form_validate ()
* @ see node_form_submit ()
* @ see node_form_submit_build_node ()
*/
2007-08-20 07:03:08 +00:00
function node_form_build_preview ( $form , & $form_state ) {
2010-11-20 06:49:47 +00:00
$node = node_form_submit_build_node ( $form , $form_state );
2007-08-20 07:03:08 +00:00
$form_state [ 'node_preview' ] = node_preview ( $node );
2010-06-17 13:44:45 +00:00
$form_state [ 'rebuild' ] = TRUE ;
2007-08-20 07:03:08 +00:00
}
/**
2011-11-28 11:44:25 +00:00
* Generates a node preview .
*
* @ param $node
* The node to preview .
*
* @ return
* Themed node preview .
*
* @ see node_form_build_preview ()
2007-08-20 07:03:08 +00:00
*/
2009-11-08 10:02:41 +00:00
function node_preview ( $node ) {
2007-08-20 07:03:08 +00:00
if ( node_access ( 'create' , $node ) || node_access ( 'update' , $node )) {
2009-08-18 06:01:07 +00:00
_field_invoke_multiple ( 'load' , 'node' , array ( $node -> nid => $node ));
2008-01-29 11:08:17 +00:00
// Load the user's name when needed.
2007-08-20 07:03:08 +00:00
if ( isset ( $node -> name )) {
// The use of isset() is mandatory in the context of user IDs, because
// user ID 0 denotes the anonymous user.
2009-03-14 23:01:38 +00:00
if ( $user = user_load_by_name ( $node -> name )) {
2007-08-20 07:03:08 +00:00
$node -> uid = $user -> uid ;
$node -> picture = $user -> picture ;
}
else {
$node -> uid = 0 ; // anonymous user
}
}
2008-10-12 04:30:09 +00:00
elseif ( $node -> uid ) {
2009-03-14 23:01:38 +00:00
$user = user_load ( $node -> uid );
2007-08-20 07:03:08 +00:00
$node -> name = $user -> name ;
$node -> picture = $user -> picture ;
}
2008-09-17 07:11:59 +00:00
$node -> changed = REQUEST_TIME ;
2009-10-16 03:21:23 +00:00
$nodes = array ( $node -> nid => $node );
field_attach_prepare_view ( 'node' , $nodes , 'full' );
2007-08-20 07:03:08 +00:00
2008-01-29 11:08:17 +00:00
// Display a preview of the node.
2007-08-20 07:03:08 +00:00
if ( ! form_get_errors ()) {
2010-07-30 03:06:18 +00:00
$node -> in_preview = TRUE ;
$output = theme ( 'node_preview' , array ( 'node' => $node ));
unset ( $node -> in_preview );
2007-08-20 07:03:08 +00:00
}
2008-10-13 00:33:05 +00:00
drupal_set_title ( t ( 'Preview' ), PASS_THROUGH );
2007-08-20 07:03:08 +00:00
return $output ;
}
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for a node preview for display during node creation and editing .
2007-08-20 07:03:08 +00:00
*
2009-10-09 01:00:08 +00:00
* @ param $variables
* An associative array containing :
* - node : The node object which is being previewed .
2007-12-06 09:58:34 +00:00
*
2011-11-28 11:44:25 +00:00
* @ see node_preview ()
2007-12-06 09:58:34 +00:00
* @ ingroup themeable
2007-08-20 07:03:08 +00:00
*/
2009-10-09 01:00:08 +00:00
function theme_node_preview ( $variables ) {
$node = $variables [ 'node' ];
2012-03-01 01:03:30 +00:00
$output = '' ;
2008-01-10 15:57:10 +00:00
2008-01-09 12:10:04 +00:00
$preview_trimmed_version = FALSE ;
2008-01-10 15:57:10 +00:00
2010-04-11 18:33:44 +00:00
$elements = node_view ( clone $node , 'teaser' );
$trimmed = drupal_render ( $elements );
$elements = node_view ( $node , 'full' );
$full = drupal_render ( $elements );
2009-06-12 08:39:40 +00:00
// Do we need to preview trimmed version of post as well as full version?
if ( $trimmed != $full ) {
2007-08-20 07:03:08 +00:00
drupal_set_message ( t ( 'The trimmed version of your post shows what your post looks like when promoted to the main page or when exported for syndication.<span class="no-js"> You can insert the delimiter "<!--break-->" (without the quotes) to fine-tune where your post gets split.</span>' ));
2008-04-14 17:48:46 +00:00
$output .= '<h3>' . t ( 'Preview trimmed version' ) . '</h3>' ;
2009-06-12 08:39:40 +00:00
$output .= $trimmed ;
2008-04-14 17:48:46 +00:00
$output .= '<h3>' . t ( 'Preview full version' ) . '</h3>' ;
2009-06-12 08:39:40 +00:00
$output .= $full ;
2007-08-20 07:03:08 +00:00
}
else {
2009-06-12 08:39:40 +00:00
$output .= $full ;
2007-08-20 07:03:08 +00:00
}
return $output ;
}
2011-11-28 11:44:25 +00:00
/**
* Form submission handler that saves the node for node_form () .
*
* @ see node_form_delete_submit ()
* @ see node_form_build_preview ()
* @ see node_form_validate ()
* @ see node_form_submit_build_node ()
*/
2007-08-20 07:03:08 +00:00
function node_form_submit ( $form , & $form_state ) {
2010-11-20 06:49:47 +00:00
$node = node_form_submit_build_node ( $form , $form_state );
2007-08-20 07:03:08 +00:00
$insert = empty ( $node -> nid );
node_save ( $node );
2008-04-14 17:48:46 +00:00
$node_link = l ( t ( 'view' ), 'node/' . $node -> nid );
2010-01-09 21:54:01 +00:00
$watchdog_args = array ( '@type' => $node -> type , '%title' => $node -> title );
$t_args = array ( '@type' => node_type_get_name ( $node ), '%title' => $node -> title );
2007-08-20 07:03:08 +00:00
if ( $insert ) {
2011-07-04 16:58:33 +00:00
watchdog ( 'content' , '@type: added %title.' , $watchdog_args , WATCHDOG_NOTICE , $node_link );
2007-12-11 12:13:39 +00:00
drupal_set_message ( t ( '@type %title has been created.' , $t_args ));
2007-08-20 07:03:08 +00:00
}
else {
2011-07-04 16:58:33 +00:00
watchdog ( 'content' , '@type: updated %title.' , $watchdog_args , WATCHDOG_NOTICE , $node_link );
2007-12-11 12:13:39 +00:00
drupal_set_message ( t ( '@type %title has been updated.' , $t_args ));
2007-08-20 07:03:08 +00:00
}
if ( $node -> nid ) {
2009-12-01 16:03:35 +00:00
$form_state [ 'values' ][ 'nid' ] = $node -> nid ;
2008-02-03 19:26:10 +00:00
$form_state [ 'nid' ] = $node -> nid ;
2008-04-14 17:48:46 +00:00
$form_state [ 'redirect' ] = 'node/' . $node -> nid ;
2007-08-20 07:03:08 +00:00
}
else {
// In the unlikely case something went wrong on save, the node will be
// rebuilt and node form redisplayed the same way as in preview.
2007-11-12 19:06:33 +00:00
drupal_set_message ( t ( 'The post could not be saved.' ), 'error' );
2010-06-17 13:44:45 +00:00
$form_state [ 'rebuild' ] = TRUE ;
2007-08-20 07:03:08 +00:00
}
2010-03-31 13:55:25 +00:00
// Clear the page and block caches.
cache_clear_all ();
2007-08-20 07:03:08 +00:00
}
/**
2010-06-17 13:44:45 +00:00
* Updates the form state 's node entity by processing this submission' s values .
*
2010-11-20 06:49:47 +00:00
* This is the default builder function for the node form . It is called
2010-06-17 13:44:45 +00:00
* during the " Save " and " Preview " submit handlers to retrieve the entity to
* save or preview . This function can also be called by a " Next " button of a
* wizard to update the form state 's entity with the current step' s values
* before proceeding to the next step .
*
* @ see node_form ()
2011-11-28 11:44:25 +00:00
* @ see node_form_delete_submit ()
* @ see node_form_build_preview ()
* @ see node_form_validate ()
* @ see node_form_submit ()
2007-08-20 07:03:08 +00:00
*/
function node_form_submit_build_node ( $form , & $form_state ) {
2010-06-17 13:44:45 +00:00
// @todo Legacy support for modules that extend the node form with form-level
// submit handlers that adjust $form_state['values'] prior to those values
// being used to update the entity. Module authors are encouraged to instead
// adjust the node directly within a hook_node_submit() implementation. For
// Drupal 8, evaluate whether the pattern of triggering form-level submit
// handlers during button-level submit processing is worth supporting
// properly, and if so, add a Form API function for doing so.
2007-08-20 07:03:08 +00:00
unset ( $form_state [ 'submit_handlers' ]);
2009-02-05 03:42:58 +00:00
form_execute_handlers ( 'submit' , $form , $form_state );
2010-06-17 13:44:45 +00:00
$node = $form_state [ 'node' ];
entity_form_submit_build_entity ( 'node' , $node , $form , $form_state );
2009-02-03 17:30:13 +00:00
2010-06-17 13:44:45 +00:00
node_submit ( $node );
foreach ( module_implements ( 'node_submit' ) as $module ) {
$function = $module . '_node_submit' ;
$function ( $node , $form , $form_state );
}
2007-08-20 07:03:08 +00:00
return $node ;
}
/**
2011-11-28 11:44:25 +00:00
* Page callback : Form constructor for node deletion confirmation form .
*
* @ see node_menu ()
2007-08-20 07:03:08 +00:00
*/
2009-11-08 10:02:41 +00:00
function node_delete_confirm ( $form , & $form_state , $node ) {
2010-02-17 05:42:42 +00:00
$form [ '#node' ] = $node ;
// Always provide entity id in the same form key as in the entity edit form.
$form [ 'nid' ] = array ( '#type' => 'value' , '#value' => $node -> nid );
2007-08-20 07:03:08 +00:00
return confirm_form ( $form ,
2010-01-09 21:54:01 +00:00
t ( 'Are you sure you want to delete %title?' , array ( '%title' => $node -> title )),
2009-10-14 20:42:47 +00:00
'node/' . $node -> nid ,
2007-08-20 07:03:08 +00:00
t ( 'This action cannot be undone.' ),
2007-11-28 10:29:21 +00:00
t ( 'Delete' ),
2007-11-27 13:31:04 +00:00
t ( 'Cancel' )
);
2007-08-20 07:03:08 +00:00
}
/**
2011-11-28 11:44:25 +00:00
* Form submission handler for node_delete_confirm () .
2007-08-20 07:03:08 +00:00
*/
function node_delete_confirm_submit ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'confirm' ]) {
2009-05-20 05:39:45 +00:00
$node = node_load ( $form_state [ 'values' ][ 'nid' ]);
2007-08-20 07:03:08 +00:00
node_delete ( $form_state [ 'values' ][ 'nid' ]);
2010-01-09 21:54:01 +00:00
watchdog ( 'content' , '@type: deleted %title.' , array ( '@type' => $node -> type , '%title' => $node -> title ));
drupal_set_message ( t ( '@type %title has been deleted.' , array ( '@type' => node_type_get_name ( $node ), '%title' => $node -> title )));
2007-08-20 07:03:08 +00:00
}
$form_state [ 'redirect' ] = '<front>' ;
}
/**
2011-11-28 11:44:25 +00:00
* Page callback : Generates an overview table of older revisions of a node .
*
* @ see node_menu ()
2007-08-20 07:03:08 +00:00
*/
2009-11-08 10:02:41 +00:00
function node_revision_overview ( $node ) {
2010-01-09 21:54:01 +00:00
drupal_set_title ( t ( 'Revisions for %title' , array ( '%title' => $node -> title )), PASS_THROUGH );
2007-08-20 07:03:08 +00:00
$header = array ( t ( 'Revision' ), array ( 'data' => t ( 'Operations' ), 'colspan' => 2 ));
$revisions = node_revision_list ( $node );
$rows = array ();
$revert_permission = FALSE ;
if (( user_access ( 'revert revisions' ) || user_access ( 'administer nodes' )) && node_access ( 'update' , $node )) {
$revert_permission = TRUE ;
}
$delete_permission = FALSE ;
2007-12-20 09:20:41 +00:00
if (( user_access ( 'delete revisions' ) || user_access ( 'administer nodes' )) && node_access ( 'delete' , $node )) {
2007-08-20 07:03:08 +00:00
$delete_permission = TRUE ;
}
foreach ( $revisions as $revision ) {
$row = array ();
$operations = array ();
if ( $revision -> current_vid > 0 ) {
2009-10-09 01:00:08 +00:00
$row [] = array ( 'data' => t ( '!date by !username' , array ( '!date' => l ( format_date ( $revision -> timestamp , 'short' ), " node/ $node->nid " ), '!username' => theme ( 'username' , array ( 'account' => $revision ))))
2008-04-14 17:48:46 +00:00
. (( $revision -> log != '' ) ? '<p class="revision-log">' . filter_xss ( $revision -> log ) . '</p>' : '' ),
2009-08-22 14:34:23 +00:00
'class' => array ( 'revision-current' ));
2010-08-17 13:50:52 +00:00
$operations [] = array ( 'data' => drupal_placeholder ( t ( 'current revision' )), 'class' => array ( 'revision-current' ), 'colspan' => 2 );
2007-08-20 07:03:08 +00:00
}
else {
2009-10-09 01:00:08 +00:00
$row [] = t ( '!date by !username' , array ( '!date' => l ( format_date ( $revision -> timestamp , 'short' ), " node/ $node->nid /revisions/ $revision->vid /view " ), '!username' => theme ( 'username' , array ( 'account' => $revision ))))
2008-04-14 17:48:46 +00:00
. (( $revision -> log != '' ) ? '<p class="revision-log">' . filter_xss ( $revision -> log ) . '</p>' : '' );
2007-08-20 07:03:08 +00:00
if ( $revert_permission ) {
$operations [] = l ( t ( 'revert' ), " node/ $node->nid /revisions/ $revision->vid /revert " );
}
if ( $delete_permission ) {
$operations [] = l ( t ( 'delete' ), " node/ $node->nid /revisions/ $revision->vid /delete " );
}
}
$rows [] = array_merge ( $row , $operations );
}
2009-12-28 11:12:55 +00:00
2009-07-29 06:39:35 +00:00
$build [ 'node_revisions_table' ] = array (
'#theme' => 'table' ,
'#rows' => $rows ,
'#header' => $header ,
2012-03-01 01:03:30 +00:00
'#attached' => array (
'css' => array ( drupal_get_path ( 'module' , 'node' ) . '/node.admin.css' ),
),
2009-07-29 06:39:35 +00:00
);
2007-08-20 07:03:08 +00:00
2009-07-29 06:39:35 +00:00
return $build ;
2007-08-20 07:03:08 +00:00
}
2007-12-03 07:45:15 +00:00
/**
2011-11-28 11:44:25 +00:00
* Page callback : Form constructor for the reversion confirmation form .
*
* This form prevents against CSRF attacks .
*
* @ see node_menu ()
* @ see node_revision_revert_confirm_submit ()
2007-12-03 07:45:15 +00:00
*/
2009-09-18 00:12:48 +00:00
function node_revision_revert_confirm ( $form , $form_state , $node_revision ) {
2007-12-03 07:45:15 +00:00
$form [ '#node_revision' ] = $node_revision ;
2008-04-14 17:48:46 +00:00
return confirm_form ( $form , t ( 'Are you sure you want to revert to the revision from %revision-date?' , array ( '%revision-date' => format_date ( $node_revision -> revision_timestamp ))), 'node/' . $node_revision -> nid . '/revisions' , '' , t ( 'Revert' ), t ( 'Cancel' ));
2007-12-03 07:45:15 +00:00
}
2011-11-28 11:44:25 +00:00
/**
* Form submission handler for node_revision_revert_confirm () .
*/
2007-12-03 07:45:15 +00:00
function node_revision_revert_confirm_submit ( $form , & $form_state ) {
$node_revision = $form [ '#node_revision' ];
$node_revision -> revision = 1 ;
$node_revision -> log = t ( 'Copy of the revision from %date.' , array ( '%date' => format_date ( $node_revision -> revision_timestamp )));
node_save ( $node_revision );
2010-01-09 21:54:01 +00:00
watchdog ( 'content' , '@type: reverted %title revision %revision.' , array ( '@type' => $node_revision -> type , '%title' => $node_revision -> title , '%revision' => $node_revision -> vid ));
drupal_set_message ( t ( '@type %title has been reverted back to the revision from %revision-date.' , array ( '@type' => node_type_get_name ( $node_revision ), '%title' => $node_revision -> title , '%revision-date' => format_date ( $node_revision -> revision_timestamp ))));
2008-04-14 17:48:46 +00:00
$form_state [ 'redirect' ] = 'node/' . $node_revision -> nid . '/revisions' ;
2007-12-03 07:45:15 +00:00
}
2007-08-20 07:03:08 +00:00
2011-11-28 11:44:25 +00:00
/**
* Page callback : Form constructor for the revision deletion confirmation form .
*
* This form prevents against CSRF attacks .
*
* @ see node_menu ()
* @ see node_revision_delete_confirm_submit ()
*/
2009-09-18 00:12:48 +00:00
function node_revision_delete_confirm ( $form , $form_state , $node_revision ) {
2007-12-03 07:45:15 +00:00
$form [ '#node_revision' ] = $node_revision ;
2008-04-14 17:48:46 +00:00
return confirm_form ( $form , t ( 'Are you sure you want to delete the revision from %revision-date?' , array ( '%revision-date' => format_date ( $node_revision -> revision_timestamp ))), 'node/' . $node_revision -> nid . '/revisions' , t ( 'This action cannot be undone.' ), t ( 'Delete' ), t ( 'Cancel' ));
2007-12-03 07:45:15 +00:00
}
2011-11-28 11:44:25 +00:00
/**
* Form submission handler for node_revision_delete_confirm () .
*/
2007-12-03 07:45:15 +00:00
function node_revision_delete_confirm_submit ( $form , & $form_state ) {
$node_revision = $form [ '#node_revision' ];
2009-09-25 14:24:34 +00:00
node_revision_delete ( $node_revision -> vid );
2010-01-09 21:54:01 +00:00
watchdog ( 'content' , '@type: deleted %title revision %revision.' , array ( '@type' => $node_revision -> type , '%title' => $node_revision -> title , '%revision' => $node_revision -> vid ));
drupal_set_message ( t ( 'Revision from %revision-date of @type %title has been deleted.' , array ( '%revision-date' => format_date ( $node_revision -> revision_timestamp ), '@type' => node_type_get_name ( $node_revision ), '%title' => $node_revision -> title )));
2008-04-14 17:48:46 +00:00
$form_state [ 'redirect' ] = 'node/' . $node_revision -> nid ;
2009-04-25 16:33:48 +00:00
if ( db_query ( 'SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid' , array ( ':nid' => $node_revision -> nid )) -> fetchField () > 1 ) {
2007-12-03 07:45:15 +00:00
$form_state [ 'redirect' ] .= '/revisions' ;
}
}