You are here

function _vkxp_process_node_settings_form in VK CrossPoster 7.2

Same name and namespace in other branches
  1. 6.3 vkxp.forms.inc \_vkxp_process_node_settings_form()

Add vkxp settings to node settings form.

Parameters

$form: Node settings form.

1 call to _vkxp_process_node_settings_form()
vkxp_form_node_type_form_alter in ./vkxp.module
Implements hook_form_FORM_ID_alter(). Add module settings to the node settings form.

File

./vkxp.forms.inc, line 14
vkxp.forms.inc Contains node form alters.

Code

function _vkxp_process_node_settings_form(&$form) {

  // Do not process node form if it do not contain node type.
  if (empty($form['#node_type']->type)) {
    return;
  }
  $node_type = $form['#node_type']->type;
  $form['vkxp'] = array(
    '#type' => 'fieldset',
    '#title' => t('VKontakte CrossPoster settings'),
    '#group' => 'additional_settings',
  );

  // Enable setting.
  $form['vkxp']['vkxp_node_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable VKontakte CrossPoster'),
    '#default_value' => variable_get('vkxp_node_enabled_' . $node_type),
    '#description' => t('Enable or disable crosspost for this node type.'),
  );

  // Enable default setting.
  $form['vkxp']['vkxp_node_enabled_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Checkbox "Post this node to VK" are checked by default'),
    '#default_value' => variable_get('vkxp_node_enabled_default_' . $node_type),
    '#description' => t('Check this if you want checkbox "Post this node to VK" in node forms was checked by default.'),
  );
  $form['vkxp']['vkxp_node_enabled_prevent_double_post'] = array(
    '#type' => 'checkbox',
    '#title' => t('Uncheck previous checkbox if current node has already posted to VK'),
    '#description' => t('Prevent double posting for nodes that already sent to VK.'),
    '#default_value' => variable_get('vkxp_node_enabled_prevent_double_post_' . $node_type),
    '#states' => array(
      'visible' => array(
        ':input[name="vkxp_node_enabled_default"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Get information about node fields.
  $fields_info = field_info_instances('node', $node_type);

  // Collect all text and image fields.
  $text_fields = array(
    'title' => t('Title'),
  );
  $image_fields = array(
    0 => t('- None -'),
  );
  $hashtag_fields = array(
    0 => t('- None -'),
  );
  $image_field_types = array(
    'image',
  );
  foreach ($fields_info as $field_name => $field_instance) {
    $field_initial_settings = field_info_field($field_name);
    if ($field_instance['widget']['module'] == 'text') {
      $text_fields[$field_name] = $field_instance['label'] . ' (' . $field_name . ')';
    }
    elseif (in_array($field_initial_settings['type'], $image_field_types)) {
      $image_fields[$field_name] = $field_instance['label'] . ' (' . $field_name . ')';
    }
    if ($field_initial_settings['type'] == 'taxonomy_term_reference') {
      $hashtag_fields[$field_name] = $field_instance['label'] . ' (' . $field_name . ')';
    }
  }

  // Message.
  $form['vkxp']['vkxp_body'] = array(
    '#type' => 'fieldset',
    '#title' => t('Message'),
  );

  // Message field.
  $form['vkxp']['vkxp_body']['vkxp_node_message_field'] = array(
    '#type' => 'textarea',
    '#title' => t('Message template'),
    '#description' => t('Message template, written by token.'),
    '#default_value' => variable_get('vkxp_node_message_field_' . $node_type, 'title'),
  );
  if (module_exists('token')) {
    $form['vkxp']['vkxp_body']['token_help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'node',
      ),
    );
  }
  else {
    $form['vkxp']['vkxp_body']['token_help'] = array(
      '#markup' => t('This module use token for configuration of message template. Please enable token module for better configuration.'),
    );
  }

  // Message length.
  $form['vkxp']['vkxp_body']['vkxp_node_message_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Cut message'),
    '#field_prefix' => t('Cut message if it has more than'),
    '#field_suffix' => t('symbols'),
    '#size' => 4,
    '#default_value' => variable_get('vkxp_node_message_length_' . $node_type, 255),
    '#description' => t('Leave this field empty to not cut a message.'),
    '#attributes' => array(
      'style' => 'width:auto;',
    ),
  );

  // Image.
  $form['vkxp']['vkxp_image'] = array(
    '#type' => 'fieldset',
    '#title' => t('Image'),
  );

  // Image source.
  $form['vkxp']['vkxp_image']['vkxp_node_image_field'] = array(
    '#type' => 'select',
    '#title' => t('Image source'),
    '#description' => t('Select field from which to take the image to crosspost.'),
    '#options' => $image_fields,
    '#default_value' => variable_get('vkxp_node_image_field_' . $node_type, 0),
  );

  // We don't need this option if image fields is not presented.
  if (!empty($image_fields)) {
    $image_styles = image_styles();
    if (!empty($image_styles)) {
      $styles[] = t('Choose a style');
      foreach ($image_styles as $style => $style_info) {
        $styles[$style] = $style_info['label'];
      }

      // Adds an image style selector.
      $form['vkxp']['vkxp_image']['vkxp_node_image_field_style'] = array(
        '#type' => 'select',
        '#title' => t('Image style'),
        '#description' => t('Select image style to apply it to image which will be send into VK.'),
        '#options' => $styles,
        '#default_value' => variable_get('vkxp_node_image_field_style_' . $node_type, 0),
        '#states' => array(
          'invisible' => array(
            'select[name="vkxp_node_image_field"]' => array(
              'value' => 0,
            ),
          ),
        ),
      );
    }
  }

  // Image limit.
  $form['vkxp']['vkxp_image']['vkxp_node_image_limit'] = array(
    '#type' => 'select',
    '#title' => t('Images limit'),
    '#description' => t('Select maximum amount of images to crosspost.'),
    '#options' => drupal_map_assoc(range(0, 10)),
    '#default_value' => variable_get('vkxp_node_image_limit_' . $node_type, 0),
  );

  //Hashtag.
  $form['vkxp']['vkxp_hashtags'] = array(
    '#type' => 'fieldset',
    '#title' => t('Hashtags'),
  );
  $form['vkxp']['vkxp_hashtags']['vkxp_node_hashtag_field'] = array(
    '#type' => 'select',
    '#title' => t('Tags field'),
    '#description' => t('Select field from which to take hashtags to crosspost.'),
    '#options' => $hashtag_fields,
    '#default_value' => variable_get('vkxp_node_hashtag_field_' . $node_type, 0),
  );
}