1

Picture 1

Picture 2

Please see the attached pictures. In one of my projects I have a taxonomy, which users can edit and add data. It has multiple tabs (see picture one) and each tab has multiple fields in which paragraphs can be added (see picture two).

We have different kinds of paragraphs with each different titles. Our problem is that we want to show each paragraph to the end-user (see the second picture, the button "Meting (decimaal) toevoegen") with a title of "meting" while maintaining the more descriptive titles for the admins and developers.

One option is changing the title for the end-user while maintaining the actual title in the DB. The other one would be changing the actual title in the DB and change it for the admins (setting the system name as the title).

and I've tried all kinds of hooks but can't wrap my head around this problem.

Solution: Due to the structure of the site, I couldn't get any hook solutions to work. So I wrote my own module, which attaches the javascript below to the form.

(function ($) {
  'use strict';

  Drupal.behaviors.test_form_styles = {
    attach: function (context, settings) {
      var regEx = /(?:meting)/i;
      var paragraphTitles = document.getElementsByClassName("paragraph-type-title");
      Object.keys(paragraphTitles).forEach(function (key) {
        if (regEx.test(paragraphTitles[key].innerHTML)){
          paragraphTitles[key].innerHTML = "Meting";
        }
      });

      var submitButtons = document.getElementsByClassName("field-add-more-submit");
      Object.keys(submitButtons).forEach(function (key) {
        if (regEx.test(submitButtons[key].value)) {
          submitButtons[key].value = "Meting toevoegen";
        }
      });
    }
  }
} (jQuery));
  • There are many different directions you can take to "solve" this, it all depends on why you need to make this change. If users understand the shorter version, why would admins need it with more info? Maybe you can add a custom permissions field for admins that label it a certain way? You could use String Overrides drupal.org/project/stringoverrides/issues/1355174, this answer is relevant: drupal.stackexchange.com/questions/233142/… Your best bet is probably a custom module with hook form alter and user role condition. – prkos Apr 13 at 20:36
  • @prkos the admins need the more descriptive version for when they want to make changes in the paragraphs. It's hard selecting the right paragraph if 8 of them are named the same. For the end-users we want to provide them with the most friendly interface. Unfortunally stringoverrides is only available for 7.x and not 8.x. I've tried hook_form_alter, but I can't find a way to target the right paragraph fields and change their title. – tail-my-error Apr 13 at 21:39
  • AFAICT there has been work done on String overrides for D8 so you can check that out. Also try searching for hook form alter questions, or ask a new question targeting that specific scenario. – prkos Apr 13 at 21:46
0

If you want to change the title of certain paragraphs in the form:

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function mymodule_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
  if ($element['#paragraph_type'] == 'foo') {
    // insert more conditions
    $element['top']['paragraph_type_title']['info']['#markup'] = t('New title for paragraph type Foo');
  }
}
  • thanks! I've tried using the hook, but since I use paragraphs placed in other paragraphs with multiple types of paragraphs, I wasn't able to get it to work. In the end I used a javascript solution (see editted question). – tail-my-error Apr 16 at 11:42
  • Perfect, this answer is for the question title others will find when googling and then they have the javascript solution for more advanced cases. – 4k4 Apr 16 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.