class NodeBlock extends Block {
function getAdminTitle() {
if (!empty($this->settings['admin_label'])) {
return check_plain($this->settings['admin_label']);
}
return t('Existing content');
}
function getAdminPreview() {
if (!empty($this->settings['admin_description'])) {
return filter_xss($this->settings['admin_description']);
}
$preview = '<p>' . t('Displays content of node ID: @nid', array('@nid' => $this->settings['nid'])) . '</p>';
$preview .= $this->getAdminConditionsPreview();
return $preview;
}
function getTitle() {
$title = NULL;
if (module_exists('translation') && !empty($this->settings['translate'])) {
$node = $this->loadTranslatedNode();
}
else {
$node = node_load($this->settings['nid']);
}
if ($this->settings['title_display'] === LAYOUT_TITLE_CUSTOM && $this->settings['title']) {
$title = check_plain($this->settings['title']);
}
elseif ($this->settings['title_display'] === LAYOUT_TITLE_DEFAULT) {
$title = $node->title;
}
if ($title && $this->settings['link_node_title']) {
$title = l($title, 'node/' . $node->nid);
}
else {
$title = check_plain($title);
}
return $title;
}
function getContent() {
if (module_exists('translation') && !empty($this->settings['translate'])) {
$node = $this->loadTranslatedNode();
}
else {
$node = node_load($this->settings['nid']);
}
if (!node_access('view', $node)) {
return;
}
$clone = clone($node);
if (empty($this->settings['leave_node_title'])) {
$clone->title = '';
}
$content = node_view($clone, $this->settings['view_mode']);
if (empty($this->settings['links'])) {
$content['links']['#access'] = FALSE;
}
$content['#display_submitted'] = !empty($this->settings['display_submitted']);
$content['#theme'] .= '__node_block';
return $content;
}
function loadTranslatedNode() {
global $language;
$node = node_load($this->settings['nid']);
if (!empty($node->tnid) && !empty($node->langcode)) {
$translations = translation_node_get_translations($node->tnid);
foreach ($translations as $code => $translation) {
if ($code == $language->langcode) {
return node_load($translation->nid);
}
}
}
return $node;
}
function form(&$form, &$form_state) {
parent::form($form, $form_state);
$form['title_display']['title_display']['#options'][LAYOUT_TITLE_DEFAULT] = t('Use content title');
$form['title_display']['title_display']['#weight'] = -50;
$settings = $this->settings;
$form['link_node_title'] = array(
'#type' => 'checkbox',
'#default_value' => !empty($settings['link_node_title']),
'#title' => t('Link block title to full-page display of embedded content.'),
'#states' => array(
'invisible' => array(
':input[name="title_display"]' => array('value' => LAYOUT_TITLE_NONE),
),
),
);
$form['leave_node_title'] = array(
'#type' => 'checkbox',
'#default_value' => !empty($settings['leave_node_title']),
'#title' => t('Show the content title inside of the block.'),
'#states' => array(
'invisible' => array(
':input[name="title_display"]' => array('value' => LAYOUT_TITLE_DEFAULT),
),
),
);
$nid_default = '';
if (!empty($settings['nid'])) {
$title = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $settings['nid']))->fetchField();
$nid_default = $title . ' [' . $settings['nid'] . ']';
}
$form['nid'] = array(
'#title' => t('Content title or ID'),
'#type' => 'textfield',
'#maxlength' => 512,
'#default_value' => $nid_default,
'#autocomplete_path' => 'node/autocomplete',
);
$entity = entity_get_info('node');
$view_mode_options = array();
foreach ($entity['view modes'] as $mode => $option) {
$view_mode_options[$mode] = $option['label'];
}
unset($view_mode_options['rss']);
unset($view_mode_options['search_index']);
unset($view_mode_options['search_result']);
$form['view_mode'] = array(
'#title' => t('Display mode'),
'#type' => 'select',
'#options' => $view_mode_options,
'#default_value' => isset($settings['view_mode']) ? $settings['view_mode'] : 'full',
);
$form['display_submitted'] = array(
'#type' => 'checkbox',
'#default_value' => !empty($settings['display_submitted']),
'#title' => t('Show author and date information'),
);
$form['links'] = array(
'#type' => 'checkbox',
'#default_value' => !empty($settings['links']),
'#title' => t('Include the links "add comment", "read more" etc.'),
);
if (module_exists('translation')) {
$form['translate'] = array(
'#type' => 'checkbox',
'#default_value' => !empty($settings['translate']),
'#title' => t('Load translated version of the content if available'),
);
}
}
function formValidate($form, &$form_state) {
parent::formValidate($form, $form_state);
module_load_include('inc', 'node', 'node.pages');
if ($nid = node_autocomplete_validate($form_state['values']['nid'])) {
$form_state['values']['nid'] = $nid;
}
else {
form_error($form['nid'], t('Sorry, no content matching <em>%title</em> was found.', array('%title' => $form_state['values']['nid'])));
}
}
function formSubmit($form, &$form_state) {
parent::formSubmit($form, $form_state);
if ($form_state['values']['title_display'] == LAYOUT_TITLE_DEFAULT) {
$form_state['values']['leave_node_title'] = FALSE;
}
elseif ($form_state['values']['title_display'] == LAYOUT_TITLE_NONE) {
$form_state['values']['link_node_title'] = FALSE;
}
$this->settings['nid'] = (int) $form_state['values']['nid'];
$this->settings['links'] = (bool) $form_state['values']['links'];
$this->settings['leave_node_title'] = (bool) $form_state['values']['leave_node_title'];
$this->settings['link_node_title'] = (bool) $form_state['values']['link_node_title'];
$this->settings['display_submitted'] = (bool) $form_state['values']['display_submitted'];
$this->settings['view_mode'] = (string) $form_state['values']['view_mode'];
if (module_exists('translation')) {
$this->settings['translate'] = (bool) $form_state['values']['translate'];
}
}
}