You are here

button_field.module in Button Field 8

Same filename and directory in other branches
  1. 6 button_field.module
  2. 7 button_field.module

Defines a field, widget and formatter for the button field type.

File

button_field.module
View source
<?php

/**
 * @file
 * Defines a field, widget and formatter for the button field type.
 */
use Drupal\button_field\Event\ButtonFieldClickedEvent;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand;

/**
 * Implements button_field_views_api().
 */
function button_field_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'button_field') . '/includes/views',
  );
}

/**
 * Form builder callback for the dummy form used to render button field's on a
 * display that is not editable.
 */
function button_field_dummy_form($form, &$form_state) {

  // Set the form's language.
  $langcode = field_language($form_state['#entity_type'], $form_state['#' . $form_state['#entity_type']], $form_state['#field']['field_name']);
  $form['language']['#value'] = $langcode;

  // Add the field and the instance to the form state.
  $field_name = $form_state['#field']['field_name'];
  $form_state['field'][$field_name][$langcode]['field'] =& $form_state['#field'];
  $form_state['field'][$field_name][$langcode]['instance'] =& $form_state['#instance'];

  // Add the field element to the form.
  $form[$field_name][$langcode][0] = $form_state['#element'];
  return $form;
}

/**
 * Callback function for the FAPI ajax framework, used on edit forms.
 */
function button_field_callback_ajax(&$form, &$form_state) {
  $entity = $form_state
    ->getTriggeringElement()['#entity'];
  $field_name = $form_state
    ->getTriggeringElement()['#field_name'];
  $field = $entity
    ->getFieldDefinition($field_name);
  $event = new ButtonFieldClickedEvent($entity, [
    'field' => $field,
    'entity' => $entity,
  ]);
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher
    ->dispatch(ButtonFieldClickedEvent::EVENT_NAME, $event);
  return _button_field_ajax_response();
}

/**
 * Builds the response for an ajax callback.
 */
function _button_field_ajax_response() {
  $response = new AjaxResponse();
  $container = \Drupal::getContainer();
  $request = $container
    ->get('request_stack')
    ->getCurrentRequest();
  $rules_path = $request->attributes
    ->get('_rules_redirect_action_url');
  if (!empty($rules_path)) {

    // Remove the redirect so that we don't return an HTML response, and add an
    // ajax redirect command.
    $request->attributes
      ->remove('_rules_redirect_action_url');
    $response
      ->addCommand(new RedirectCommand($rules_path));
  }
  return $response;
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Hides the required setting and default value from the field instance settings
 * form because they do not apply to this field type.
 */
function button_field_form_field_ui_field_instance_edit_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#field']
    ->getType() == 'button_field') {

    // Hide the required field and set it to false.
    $form['instance']['required']['#access'] = FALSE;
    $form['instance']['required']['#default_value'] = FALSE;

    // No need to display a default value widget.
    $form['instance']['default_value']['#access'] = FALSE;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Hides the cardinality fields from the field settings form because it does not
 * apply to this field type.
 */
function button_field_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#field']
    ->getType() == 'button_field') {

    // Hide the cardinality and set it to one.
    $form['field']['cardinality_container']['#access'] = FALSE;
    $form['field']['cardinality_container']['cardinality']['#default_value'] = 'number';
    $form['field']['cardinality_container']['cardinality_number']['#default_value'] = 1;
  }
}

Functions

Namesort descending Description
button_field_callback_ajax Callback function for the FAPI ajax framework, used on edit forms.
button_field_dummy_form Form builder callback for the dummy form used to render button field's on a display that is not editable.
button_field_form_field_ui_field_edit_form_alter Implements hook_form_FORM_ID_alter().
button_field_form_field_ui_field_instance_edit_form_alter Implements hook_form_FORM_ID_alter().
button_field_views_api Implements button_field_views_api().
_button_field_ajax_response Builds the response for an ajax callback.