Commit a8f7916b authored by Matthew Radcliffe's avatar Matthew Radcliffe
Browse files

Issue #2807697: Complete Drupal 8.8 and greater port

parent f2b0008e
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+7 −0
Original line number Diff line number Diff line
/.idea/
/.vs/
/.vscode/

.DS_Store
Thumbs.db
phpunit.xml
+12 −0
Original line number Diff line number Diff line
@@ -5,6 +5,18 @@ rules. When the button is clicked, an AJAX call is used to run the event. Using
rules you could display a message to the user, alter some data on the entity (or
another entity) and reload the current (or any other) page.

Differences from Drupal 7 version
=================================
- Button field reload action has been removed. Use the Rules Core “Page redirect”
  action instead.
- Ajax button field redirect is not implemented.

Upgrading from Drupal 7
=======================
- Field, field formatter, and field widgets will be migrated as part of a Drupal
  Upgrade migration.
- Rules will need to be re-created.

Differences from Drupal 6 version
=================================
- Works with any fieldable entity
+4 −5
Original line number Diff line number Diff line
name: Button Field
type: module
description: 'Adds a button field type.'
description: 'Adds a button field type that triggers a rules action when clicked.'
package: Field types
version: VERSION
core: 8.x
core_version_requirement: ^8.8 || ^9
dependencies:
  - field
  - rules
  - drupal:field
  - rules:rules
+14 −100
Original line number Diff line number Diff line
@@ -4,108 +4,22 @@
 * 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;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Implements button_field_views_api().
 * Implements hook_views_data_alter().
 */
function button_field_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'button_field') . '/includes/views',
  );
function button_field_views_data_alter(&$data) {
  // Loops through fields definitions looking for button_field, and changes the
  // handler to the button_field field handler.
  foreach ($data as $table_name => &$table) {
    foreach ($table as $id => &$field) {
      if (!empty($field['field']['entity_type']) && !empty($field['field']['field_name'])) {
        $field_storage = FieldStorageConfig::loadByName($field['field']['entity_type'], $field['field']['field_name']);
        if ($field_storage && $field_storage->getType() === 'button_field') {
          $field['field']['id'] = 'button_field';
        }

/**
 * 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;
  }
}
+1 −1
Original line number Diff line number Diff line
button_field_clicked:
  label: 'User clicks a button field'
  category: 'Button field'
  context:
  context_definitions:
    field:
      type: 'entity:field_config'
      label: 'Field that was clicked'
Loading