You are here

function _button_field_build_element in Button Field 7

Builds the renderable array for a button field widget.

Parameters

string $id: Id for the field element.

array $field: The field being rendered.

array $instance: The instance of the field being rendered.

string $entity_type: The type of entity that the button field is being rendered for.

stdClass $entity: The entity that the button field is being rendered for.

Return value

array Renderable array of the button form element.

2 calls to _button_field_build_element()
button_field_field_formatter_view in ./button_field.module
Implements hook_field_formatter_view().
button_field_field_widget_form in ./button_field.module
Implements hook_field_widget_form().

File

./button_field.module, line 393
Defines a field, widget and formatter for the button field type.

Code

function _button_field_build_element($id, $field, $instance, $entity_type, $entity) {
  $class = array(
    'button_field',
  );
  if (isset($instance['additional_class'])) {
    $class = array_merge($classes, explode(' ', $instance['additional_class']));
  }

  // Check for confirmation message and build js settings if appropriate.
  if (isset($field['settings']['confirmation']) && !empty($field['settings']['confirmation'])) {

    // Add the script that will handle the confirmation before making the ajax
    // callback.
    drupal_add_js(drupal_get_path('module', 'button_field') . '/js/button_field.js');

    // Build the settings and add them to the page.
    $class[] = 'button_field_confirm';
    $settings[$id] = array(
      'confirmation' => $field['settings']['confirmation'],
    );
    drupal_add_js($settings, 'setting');
  }
  $element = array(
    '#type' => 'button',
    '#id' => $id,
    '#name' => $id,
    '#attributes' => array(
      'class' => $class,
    ),
    '#value' => isset($instance['widget']['settings']['text']) ? $instance['widget']['settings']['text'] : $instance['label'],
    // Button elements do not obey the #description index, so we need to add
    // our own suffix here.
    '#suffix' => '<div class="description">' . (!empty($instance['description']) ? $instance['description'] : '') . '</div>',
    '#entity_type' => $entity_type,
    '#entity' => $entity,
    '#ajax' => array(
      'callback' => 'button_field_callback_ajax',
    ),
  );

  // If this is an image button then we need to set the type appropriatly as
  // well as the image source.
  if ('button_field_image' == $instance['widget']['type']) {
    $element['#type'] = 'image_button';
    $element['#src'] = $instance['widget']['settings']['image_path'];
  }
  return $element;
}