You are here

function _heartbeat_plugins_template_attachments_sort_form in Heartbeat 7

Helper function to create draggable interface for heartbeat plugins.

1 call to _heartbeat_plugins_template_attachments_sort_form()
heartbeat_plugins_form_alter in modules/heartbeat_plugins/heartbeat_plugins.module
Implements hook_form_alter().

File

modules/heartbeat_plugins/heartbeat_plugins.module, line 540

Code

function _heartbeat_plugins_template_attachments_sort_form(&$form, &$form_state) {

  // Prepare the attachments for all active plugins willing to participate.
  $attachments = array();
  foreach (heartbeat_plugins_get_active_plugins() as $pluginWrapper) {
    $plugin = $pluginWrapper
      ->getPlugin();
    if ($plugin) {
      $item_attachments = $form_state['item']->attachments;

      // Create a list of attachment buttons provided by the plugins.
      if ($plugin
        ->hasButtons()) {
        $attachments['buttons'][$pluginWrapper->plugin_name] = array(
          'plugin' => $plugin,
          'values' => isset($item_attachments['buttons'][$pluginWrapper->plugin_name]) ? $item_attachments['buttons'][$pluginWrapper->plugin_name] : array(),
        );
      }

      // Create a list of attachments (content) provided by the plugins.
      if ($plugin
        ->hasContent()) {
        $attachments['content'][$pluginWrapper->plugin_name] = array(
          'plugin' => $plugin,
          'values' => isset($item_attachments['content'][$pluginWrapper->plugin_name]) ? $item_attachments['content'][$pluginWrapper->plugin_name] : array(),
        );
      }
    }
  }
  $item = $form_state['item'];
  $values = $item->attachments;

  /**
   *  Create wrapper for Button Attachments.
   */
  if (!empty($attachments['buttons'])) {
    if (!isset($form_state['item']->attachments['buttons'])) {
      $form_state['item']->attachments['buttons'] = array();
    }
    $form['attachments']['buttons'] = array(
      '#tree' => TRUE,
      '#type' => 'fieldset',
    );
    $form['attachments']['buttons']['title'] = array(
      '#markup' => t('Attachment Buttons'),
    );

    // Prepare a sorted list of Attachment Buttons.
    $items = array();
    foreach ($attachments['buttons'] as $class_name => $plugin_attachments) {
      foreach ($plugin_attachments['plugin']
        ->getAttachments($item, 'buttons') as $attachment) {
        $attachment['enabled'] = isset($values['buttons']['enabled'][$attachment['name']]) ? $values['buttons']['enabled'][$attachment['name']] : $attachment['enabled'];
        $attachment['weight'] = isset($values['buttons']['weight'][$attachment['name']]) ? $values['buttons']['weight'][$attachment['name']] : $attachment['weight'];
        $attachment['plugin'] = $plugin_attachments['plugin'];
        $items[] = $attachment;
      }
    }
    usort($items, '_heartbeat_attachment_sort_helper');
    $options = array();
    $enabled = array();
    $form['attachments']['buttons']['weight'] = array(
      '#tree' => TRUE,
    );
    foreach ($items as $attachment) {
      $key = $attachment['name'];
      $options[$key] = '';
      if ($attachment['enabled']) {
        $enabled[] = $key;
      }
      $form['attachments']['buttons']['weight'][$key] = array(
        '#type' => 'weight',
        '#title' => t('Weight for @title', array(
          '@title' => $attachment['title'],
        )),
        '#title_display' => 'invisible',
        '#default_value' => $attachment['weight'],
        '#attributes' => array(
          'class' => array(
            'heartbeat-attachment-order-weight',
          ),
        ),
      );
      $form['attachments']['buttons']['settings'][$key] = array(
        '#type' => 'container',
      );
      $form['attachments']['buttons']['name'][$key] = array(
        '#markup' => check_plain($attachment['title']),
      );
      $attachment['plugin']
        ->pluginAttachmentForm($form['attachments']['buttons']['settings'][$key], $form_state['item']->attachments['buttons'], 'buttons');
    }
    $form['attachments']['buttons']['enabled'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Enabled attachments'),
      '#title_display' => 'invisible',
      '#options' => $options,
      '#default_value' => $enabled,
    );
    $form['attachments']['buttons']['#theme'] = 'heartbeat_plugins_attachments_form';
  }

  /**
   *  Create wrapper for Attachments.
   */
  if (!empty($attachments['content'])) {
    if (!isset($form_state['item']->attachments['content'])) {
      $form_state['item']->attachments['content'] = array();
    }
    $form['attachments']['content'] = array(
      '#tree' => TRUE,
      '#type' => 'fieldset',
    );
    $form['attachments']['content']['title'] = array(
      '#markup' => t('Attachments content'),
    );

    // Prepare a sorted list of Attachment Content.
    $items = array();
    foreach ($attachments['content'] as $class_name => $plugin_attachments) {
      foreach ($plugin_attachments['plugin']
        ->getAttachments($item, 'content') as $attachment) {
        $attachment['enabled'] = isset($values['content']['enabled'][$attachment['name']]) ? $values['content']['enabled'][$attachment['name']] : $attachment['enabled'];
        $attachment['weight'] = isset($values['content']['weight'][$attachment['name']]) ? $values['content']['weight'][$attachment['name']] : $attachment['weight'];
        $attachment['plugin'] = $plugin_attachments['plugin'];
        $items[] = $attachment;
      }
    }
    usort($items, '_heartbeat_attachment_sort_helper');
    $options = array();
    $enabled = array();
    $form['attachments']['content']['weight'] = array(
      '#tree' => TRUE,
    );
    foreach ($items as $attachment) {
      $key = $attachment['name'];
      $options[$key] = '';
      if ($attachment['enabled']) {
        $enabled[] = $key;
      }
      $form['attachments']['content']['weight'][$key] = array(
        '#type' => 'weight',
        '#title' => t('Weight for @title', array(
          '@title' => $attachment['title'],
        )),
        '#title_display' => 'invisible',
        '#default_value' => $attachment['weight'],
        '#attributes' => array(
          'class' => array(
            'heartbeat-attachment-order-weight',
          ),
        ),
      );
      $form['attachments']['content']['settings'][$key] = array(
        '#type' => 'container',
      );
      $form['attachments']['content']['name'][$key] = array(
        '#markup' => check_plain($attachment['title']),
      );
      $attachment['plugin']
        ->pluginAttachmentForm($form['attachments']['content']['settings'][$key], $form_state['item']->attachments['content'], 'content');
    }
    $form['attachments']['content']['enabled'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Enabled attachments'),
      '#title_display' => 'invisible',
      '#options' => $options,
      '#default_value' => $enabled,
    );
    $form['attachments']['content']['#theme'] = 'heartbeat_plugins_attachments_form';
  }
}