class NodeFormController extends EntityFormController {
protected function prepareEntity(EntityInterface $node) {
$node_options = variable_get('node_options_' . $node->type, array(
'status',
'promote',
));
if (!isset($node->nid) || isset($node->is_new)) {
foreach (array(
'status',
'promote',
'sticky',
) as $key) {
if (!isset($node->{$key})) {
$node->{$key} = (int) in_array($key, $node_options);
}
}
global $user;
$node->uid = $user->uid;
$node->created = REQUEST_TIME;
}
else {
$node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
$node->log = NULL;
}
$node
->setNewRevision(in_array('revision', $node_options));
node_invoke($node, 'prepare');
module_invoke_all('node_prepare', $node);
}
public function form(array $form, array &$form_state, EntityInterface $node) {
$user_config = config('user.settings');
if (isset($form_state['node_preview'])) {
$form['#prefix'] = $form_state['node_preview'];
$node->in_preview = TRUE;
}
else {
unset($node->in_preview);
}
$form['#attributes']['class'][0] = drupal_html_class('node-' . $node->type . '-form');
foreach (array(
'nid',
'vid',
'uid',
'created',
'type',
) as $key) {
$form[$key] = array(
'#type' => 'value',
'#value' => isset($node->{$key}) ? $node->{$key} : NULL,
);
}
$form['changed'] = array(
'#type' => 'hidden',
'#default_value' => isset($node->changed) ? $node->changed : NULL,
);
$function = node_hook($node->type, 'form');
if ($function && ($extra = $function($node, $form_state))) {
$form = array_merge_recursive($form, $extra);
}
if (isset($form['title']) && !isset($form['title']['#weight'])) {
$form['title']['#weight'] = -5;
}
$language_configuration = module_invoke('language', 'get_default_configuration', 'node', $node->type);
$form['langcode'] = array(
'#title' => t('Language'),
'#type' => 'language_select',
'#default_value' => $node->langcode,
'#languages' => LANGUAGE_ALL,
'#access' => isset($language_configuration['language_hidden']) && !$language_configuration['language_hidden'],
);
$form['additional_settings'] = array(
'#type' => 'vertical_tabs',
'#weight' => 99,
);
$form['revision_information'] = array(
'#type' => 'details',
'#title' => t('Revision information'),
'#collapsible' => TRUE,
'#collapsed' => !$node
->isNewRevision(),
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'node-form-revision-information',
),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'node') . '/node.js',
),
),
'#weight' => 20,
'#access' => $node
->isNewRevision() || user_access('administer nodes'),
);
$form['revision_information']['revision'] = array(
'#type' => 'checkbox',
'#title' => t('Create new revision'),
'#default_value' => $node
->isNewRevision(),
'#access' => user_access('administer nodes'),
);
if (!$node
->isNewRevision()) {
$form['revision_information']['revision']['#states'] = array(
'checked' => array(
'textarea[name="log"]' => array(
'empty' => FALSE,
),
),
);
}
$form['revision_information']['log'] = array(
'#type' => 'textarea',
'#title' => t('Revision log message'),
'#rows' => 4,
'#default_value' => !empty($node->log) ? $node->log : '',
'#description' => t('Briefly describe the changes you have made.'),
);
$form['author'] = array(
'#type' => 'details',
'#access' => user_access('administer nodes'),
'#title' => t('Authoring information'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'node-form-author',
),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'node') . '/node.js',
array(
'type' => 'setting',
'data' => array(
'anonymous' => $user_config
->get('anonymous'),
),
),
),
),
'#weight' => 90,
);
$form['author']['name'] = array(
'#type' => 'textfield',
'#title' => t('Authored by'),
'#maxlength' => 60,
'#autocomplete_path' => 'user/autocomplete',
'#default_value' => !empty($node->name) ? $node->name : '',
'#weight' => -1,
'#description' => t('Leave blank for %anonymous.', array(
'%anonymous' => $user_config
->get('anonymous'),
)),
);
$form['author']['date'] = array(
'#type' => 'textfield',
'#title' => t('Authored on'),
'#maxlength' => 25,
'#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'),
)),
'#default_value' => !empty($node->date) ? $node->date : '',
);
$form['options'] = array(
'#type' => 'details',
'#access' => user_access('administer nodes'),
'#title' => t('Publishing options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#attributes' => array(
'class' => array(
'node-form-options',
),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'node') . '/node.js',
),
),
'#weight' => 95,
);
$form['options']['status'] = array(
'#type' => 'checkbox',
'#title' => t('Published'),
'#default_value' => $node->status,
);
$form['options']['promote'] = array(
'#type' => 'checkbox',
'#title' => t('Promoted to front page'),
'#default_value' => $node->promote,
);
$form['options']['sticky'] = array(
'#type' => 'checkbox',
'#title' => t('Sticky at top of lists'),
'#default_value' => $node->sticky,
);
$form += array(
'#submit' => array(),
);
return parent::form($form, $form_state, $node);
}
protected function actions(array $form, array &$form_state) {
$element = parent::actions($form, $form_state);
$node = $this
->getEntity($form_state);
$preview_mode = variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL);
$element['preview'] = array(
'#access' => $preview_mode != DRUPAL_DISABLED,
'#value' => t('Preview'),
'#validate' => array(
array(
$this,
'validate',
),
),
'#submit' => array(
array(
$this,
'submit',
),
array(
$this,
'preview',
),
),
);
$element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || !form_get_errors() && isset($form_state['node_preview']);
$element['delete']['#access'] = node_access('delete', $node);
return $element;
}
public function validate(array $form, array &$form_state) {
$node = $this
->buildEntity($form, $form_state);
if (isset($node->nid) && node_last_changed($node->nid) > $node->changed) {
form_set_error('changed', t('The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.'));
}
if (!empty($node->name) && !($account = user_load_by_name($node->name))) {
form_set_error('name', t('The username %name does not exist.', array(
'%name' => $node->name,
)));
}
$date = new DrupalDateTime($node->date);
if ($date
->hasErrors()) {
form_set_error('date', t('You have to specify a valid date.'));
}
if ($function = node_hook($node->type, 'validate')) {
$function($node, $form, $form_state);
}
foreach (module_implements('node_validate') as $module) {
$function = $module . '_node_validate';
$function($node, $form, $form_state);
}
parent::validate($form, $form_state);
}
public function submit(array $form, array &$form_state) {
$node = parent::submit($form, $form_state);
if (!empty($form_state['values']['revision'])) {
$node
->setNewRevision();
}
node_submit($node);
foreach (module_implements('node_submit') as $module) {
$function = $module . '_node_submit';
$function($node, $form, $form_state);
}
return $node;
}
public function preview(array $form, array &$form_state) {
drupal_set_title(t('Preview'), PASS_THROUGH);
$form_state['node_preview'] = node_preview($this
->getEntity($form_state));
$form_state['rebuild'] = TRUE;
}
public function save(array $form, array &$form_state) {
$node = $this
->getEntity($form_state);
$insert = empty($node->nid);
$node
->save();
$node_link = l(t('view'), 'node/' . $node->nid);
$watchdog_args = array(
'@type' => $node->type,
'%title' => $node
->label(),
);
$t_args = array(
'@type' => node_get_type_label($node),
'%title' => $node
->label(),
);
if ($insert) {
watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
drupal_set_message(t('@type %title has been created.', $t_args));
}
else {
watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
drupal_set_message(t('@type %title has been updated.', $t_args));
}
if ($node->nid) {
$form_state['values']['nid'] = $node->nid;
$form_state['nid'] = $node->nid;
$form_state['redirect'] = node_access('view', $node) ? 'node/' . $node->nid : '<front>';
}
else {
drupal_set_message(t('The post could not be saved.'), 'error');
$form_state['rebuild'] = TRUE;
}
cache_invalidate_tags(array(
'content' => TRUE,
));
}
public function delete(array $form, array &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$node = $this
->getEntity($form_state);
$form_state['redirect'] = array(
'node/' . $node->nid . '/delete',
array(
'query' => $destination,
),
);
}
}