content_translation_test.module

Same filename in other branches
  1. 9 core/modules/content_translation/tests/modules/content_translation_test/content_translation_test.module
  2. 8.9.x core/modules/content_translation/tests/modules/content_translation_test/content_translation_test.module
  3. 10 core/modules/content_translation/tests/modules/content_translation_test/content_translation_test.module

File

core/modules/content_translation/tests/modules/content_translation_test/content_translation_test.module

View source
<?php


/**
 * @file
 * Helper module for the Content Translation tests.
 */
declare (strict_types=1);
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_entity_bundle_info_alter().
 */
function content_translation_test_entity_bundle_info_alter(&$bundles) {
    // Store the initial status of the "translatable" property for the
    // "entity_test_mul" bundle.
    $translatable = !empty($bundles['entity_test_mul']['entity_test_mul']['translatable']);
    \Drupal::state()->set('content_translation_test.translatable', $translatable);
    // Make it translatable if Content Translation did not. This will make the
    // entity object translatable even if it is disabled in Content Translation
    // settings.
    if (!$translatable) {
        $bundles['entity_test_mul']['entity_test_mul']['translatable'] = TRUE;
    }
}

/**
 * Implements hook_entity_access().
 */
function content_translation_test_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
    $access = \Drupal::state()->get('content_translation.entity_access.' . $entity->getEntityTypeId());
    if (!empty($access[$operation])) {
        return AccessResult::allowed();
    }
    else {
        return AccessResult::neutral();
    }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 *
 * Adds a textfield to node forms based on a request parameter.
 */
function content_translation_test_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    $langcode = $form_state->getFormObject()
        ->getFormLangcode($form_state);
    if (in_array($langcode, [
        'en',
        'fr',
    ]) && \Drupal::request()->get('test_field_only_en_fr')) {
        $form['test_field_only_en_fr'] = [
            '#type' => 'textfield',
            '#title' => 'Field only available on the english and french form',
        ];
        foreach (array_keys($form['actions']) as $action) {
            if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
                $form['actions'][$action]['#submit'][] = 'content_translation_test_form_node_form_submit';
            }
        }
    }
}

/**
 * Form submission handler for custom field added based on a request parameter.
 *
 * @see content_translation_test_form_node_article_form_alter()
 */
function content_translation_test_form_node_form_submit($form, FormStateInterface $form_state) {
    \Drupal::state()->set('test_field_only_en_fr', $form_state->getValue('test_field_only_en_fr'));
}

/**
 * Implements hook_entity_translation_delete().
 */
function content_translation_test_entity_translation_delete(EntityInterface $translation) {
    \Drupal::state()->set('content_translation_test.translation_deleted', TRUE);
}

Functions

Title Deprecated Summary
content_translation_test_entity_access Implements hook_entity_access().
content_translation_test_entity_bundle_info_alter Implements hook_entity_bundle_info_alter().
content_translation_test_entity_translation_delete Implements hook_entity_translation_delete().
content_translation_test_form_node_form_alter Implements hook_form_BASE_FORM_ID_alter().
content_translation_test_form_node_form_submit Form submission handler for custom field added based on a request parameter.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.