save_edit.module in Save & Edit 8
Same filename and directory in other branches
Contains save_edit.module..
File
save_edit.moduleView source
<?php
/**
* @file
* Contains save_edit.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeForm;
/**
* Implements hook_help().
*/
function save_edit_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the save_edit module.
case 'help.page.save_edit':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Gives a "Save & Edit" button on node pages.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_alter().
*/
function save_edit_form_alter(&$form, FormStateInterface $form_state) {
$form_object = $form_state
->getFormObject();
$user = \Drupal::currentUser();
if ($user
->hasPermission('use save and edit') && $form_object instanceof NodeForm) {
$config = \Drupal::config('save_edit.settings');
$entity = $form_object
->getEntity();
$content_type = $entity
->getType();
$enabled_node_types = !is_null($config
->get('node_types')) ? $config
->get('node_types') : [];
if (in_array($content_type, array_values($enabled_node_types), TRUE)) {
$form['actions']['save_edit'] = [
'#type' => 'submit',
'#value' => $config
->get('button_value'),
'#name' => 'save_edit',
'#submit' => [
'::submitForm',
'save_edit_form_submit_presave',
'::save',
'save_edit_form_submit_redirect',
],
'#weight' => $config
->get('button_weight'),
];
if ($config
->get('dropbutton')) {
$form['actions']['save_edit']['#dropbutton'] = 'save';
}
if ($config
->get('hide_default_save')) {
$form['actions']['unpublish']['#access'] = FALSE;
$form['actions']['submit']['#access'] = FALSE;
}
if ($config
->get('hide_default_publish')) {
$form['actions']['publish']['#access'] = FALSE;
}
if ($config
->get('hide_default_preview')) {
$form['actions']['preview']['#access'] = FALSE;
}
if ($config
->get('hide_default_delete')) {
$form['actions']['delete']['#access'] = FALSE;
}
}
}
}
/**
* The published status needs to be altered before the node is saved.
*/
function save_edit_form_submit_presave(&$form, FormStateInterface $form_state) {
$config = \Drupal::config('save_edit.settings');
$entity = $form_state
->getFormObject()
->getEntity();
if ($config
->get('unpublish') || $config
->get('unpublish_new_only') && $entity
->isNew()) {
$entity
->setPublished(FALSE);
}
}
/**
* We want to override the default redirect that was set in the saving process.
*/
function save_edit_form_submit_redirect(&$form, FormStateInterface $form_state) {
$entity = $form_state
->getFormObject()
->getEntity();
$toUrl = $entity
->toUrl('edit-form');
if ($destination = \Drupal::request()->query
->get('destination')) {
$toUrl
->setRouteParameter("destination", $destination);
}
\Drupal::request()->query
->remove('destination');
$form_state
->setRedirectUrl($toUrl);
}
Functions
Name | Description |
---|---|
save_edit_form_alter | Implements hook_form_alter(). |
save_edit_form_submit_presave | The published status needs to be altered before the node is saved. |
save_edit_form_submit_redirect | We want to override the default redirect that was set in the saving process. |
save_edit_help | Implements hook_help(). |