You are here

shortcode_embed_content.module in Shortcode 7.2

File

shortcode_embed_content/shortcode_embed_content.module
View source
<?php

/*
 * FIXME: Need implement dynamic embedding: make embedding when the content shows
 */

/**
 * Implements hook_shortcode_info()
 *
 * @return array - The shortcode definition array
 *
 */
function shortcode_embed_content_shortcode_info() {
  $shortcodes['embed_content'] = array(
    'title' => t('Embed content'),
    'description' => t('Embed node content into the text.'),
    'process callback' => 'shortcode_embed_content_shortcode_node',
    //'settings callback' => '_shortcode_settings_form', TODO
    'tips callback' => 'shortcode_embed_content_node_tip',
    'attributes callback' => 'shortcode_embed_content_attributes',
    'default settings' => array(),
  );
  return $shortcodes;
}

/**
 * Attributes form element for embed_content shortcode.
 *
 * Used by shortcode_wysiwyg module to add form elements for attributes.
 */
function shortcode_embed_content_attributes($form, &$form_state) {
  $form['embed_content-nid'] = array(
    '#title' => t('Node nid'),
    '#type' => 'textfield',
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array(
          'value' => 'embed_content',
        ),
      ),
    ),
  );
  $form['embed_content-class'] = array(
    '#title' => t('Class'),
    '#type' => 'textfield',
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array(
          'value' => 'embed_content',
        ),
      ),
    ),
  );
  $form['embed_content-view_mode'] = array(
    '#title' => t('View mode'),
    '#type' => 'textfield',
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array(
          'value' => 'embed_content',
        ),
      ),
    ),
  );
  $form['embed_content-show_title'] = array(
    '#title' => t('Show title'),
    '#type' => 'select',
    '#options' => array(
      'true' => 'True',
      'false' => 'False',
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array(
          'value' => 'embed_content',
        ),
      ),
    ),
  );
  $form['embed_content-show_submitted'] = array(
    '#title' => t('Show submitted'),
    '#type' => 'select',
    '#options' => array(
      'true' => 'True',
      'false' => 'False',
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array(
          'value' => 'embed_content',
        ),
      ),
    ),
  );
  $form['embed_content-show_meta'] = array(
    '#title' => t('Show meta'),
    '#type' => 'select',
    '#options' => array(
      'true' => 'True',
      'false' => 'False',
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array(
          'value' => 'embed_content',
        ),
      ),
    ),
  );
  $form['embed_content-show_terms'] = array(
    '#title' => t('Show terms'),
    '#type' => 'select',
    '#options' => array(
      'true' => 'True',
      'false' => 'False',
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array(
          'value' => 'embed_content',
        ),
      ),
    ),
  );
  $form['embed_content-show_links'] = array(
    '#title' => t('Show links'),
    '#type' => 'select',
    '#options' => array(
      'true' => 'True',
      'false' => 'False',
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array(
          'value' => 'embed_content',
        ),
      ),
    ),
  );
  return $form;
}

/**
 * Embed a node content into the text
 *
 * Calling
 * [embed_content nid=X /]
 *
 * Other shortcode parameters
 *
 * class - additional class to the node
 *
 * If no nid given the result will be empty
 *
 * TODO: implement view_mode - other than teaser or full!
 */
function shortcode_embed_content_shortcode_node($attrs, $text) {
  $attrs = shortcode_attrs(array(
    'nid' => 0,
    'class' => '',
    'view_mode' => 'teaser',
    // TODO: actually the full mode is does not work
    'show_title' => FALSE,
    'show_submitted' => FALSE,
    'show_meta' => FALSE,
    'show_terms' => FALSE,
    'show_links' => FALSE,
  ), $attrs);
  if (!empty($attrs['nid'])) {
    $node = node_load($attrs['nid']);

    // add embedded flag to the node
    if (empty($node) || !$node->status || !node_access('view', $node)) {
      return '';
    }
    $node->shortcode_embedded = TRUE;
  }
  else {
    return '';
  }
  $params = new stdClass();
  $params->embedded = TRUE;
  $params->view_mode = $params->original_view_mode = $attrs['view_mode'];
  $params->show_title = shortcode_bool($attrs['show_title']);
  $params->show_meta = shortcode_bool($attrs['show_meta']);
  $params->show_submitted = shortcode_bool($attrs['show_submitted']);
  $params->show_terms = shortcode_bool($attrs['show_terms']);
  $params->show_links = shortcode_bool($attrs['show_links']);

  // insert the original attributes to the params
  $params->attrs = $attrs;
  $node->shortcode = $params;
  if ('full' == $attrs['view_mode']) {
    $params->view_mode = 'teaser';
  }
  $content = node_view($node, $params->view_mode);
  return render($content);
}

// shortcode_embed_content_shortcode_node()
function shortcode_embed_content_node_tip($format, $long) {
  $output = '';
  $output = '<p><strong>[embed_content nid=nodeid (class="additional class")][/embed_content]</strong> ';
  if ($long) {
    $output .= 'Embed a node by nid into the text. Additional class names can be added by the <em>class</em> parameter.</p>';
  }
  else {
    $output .= 'Embed a node by nid into the text. Additional class names can be added by the <em>class</em> parameter.</p>';
  }
  return $output;
}

// shortcode_embed_content_node_tip()

/**
 * Implementation of hook_preprocess_node()
 * TODO: implement dynamic embedding!
 */
function shortcode_embed_content_preprocess_node(&$vars) {
  if (isset($vars['shortcode']) && $vars['shortcode']->embedded) {
    $vars['template_files'][] = "node-shortcode--default";
    $vars['template_files'][] = 'node-shortcode--' . $vars['type'];
  }
}

Functions

Namesort descending Description
shortcode_embed_content_attributes Attributes form element for embed_content shortcode.
shortcode_embed_content_node_tip
shortcode_embed_content_preprocess_node Implementation of hook_preprocess_node() TODO: implement dynamic embedding!
shortcode_embed_content_shortcode_info Implements hook_shortcode_info()
shortcode_embed_content_shortcode_node Embed a node content into the text