content_translation_test.module 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for the Content Translation tests.
  5. */
  6. use Drupal\Core\Access\AccessResult;
  7. use Drupal\Core\Entity\EntityInterface;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. /**
  11. * Implements hook_entity_bundle_info_alter().
  12. */
  13. function content_translation_test_entity_bundle_info_alter(&$bundles) {
  14. // Store the initial status of the "translatable" property for the
  15. // "entity_test_mul" bundle.
  16. $translatable = !empty($bundles['entity_test_mul']['entity_test_mul']['translatable']);
  17. \Drupal::state()->set('content_translation_test.translatable', $translatable);
  18. // Make it translatable if Content Translation did not. This will make the
  19. // entity object translatable even if it is disabled in Content Translation
  20. // settings.
  21. if (!$translatable) {
  22. $bundles['entity_test_mul']['entity_test_mul']['translatable'] = TRUE;
  23. }
  24. }
  25. /**
  26. * Implements hook_entity_access().
  27. */
  28. function content_translation_test_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  29. $access = \Drupal::state()->get('content_translation.entity_access.' . $entity->getEntityTypeId());
  30. if (!empty($access[$operation])) {
  31. return AccessResult::allowed();
  32. }
  33. else {
  34. return AccessResult::neutral();
  35. }
  36. }
  37. /**
  38. * Implements hook_form_BASE_FORM_ID_alter().
  39. *
  40. * Adds a textfield to node forms based on a request parameter.
  41. */
  42. function content_translation_test_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  43. $langcode = $form_state->getFormObject()->getFormLangcode($form_state);
  44. if (in_array($langcode, ['en', 'fr']) && \Drupal::request()->get('test_field_only_en_fr')) {
  45. $form['test_field_only_en_fr'] = [
  46. '#type' => 'textfield',
  47. '#title' => 'Field only available on the english and french form',
  48. ];
  49. foreach (array_keys($form['actions']) as $action) {
  50. if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
  51. $form['actions'][$action]['#submit'][] = 'content_translation_test_form_node_form_submit';
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * Form submission handler for custom field added based on a request parameter.
  58. *
  59. * @see content_translation_test_form_node_article_form_alter()
  60. */
  61. function content_translation_test_form_node_form_submit($form, FormStateInterface $form_state) {
  62. \Drupal::state()->set('test_field_only_en_fr', $form_state->getValue('test_field_only_en_fr'));
  63. }