Hi everyone,

I'm having compatibility problems with the Translation Management Tool. The code needs at least a couple of condition check in the better_formats_filter_process_format function.

 if ($form_object instanceof Drupal\Core\Entity\ContentEntityForm) {
    $entity = $element['#entity'];
    $entity_type = $entity->getEntityTypeId();
    $field_name = $element['#field_name'];
    $field_definition = $entity->getFieldDefinition($field_name);

The $entity['#entity'] value doesn't exists in some cases.

Thank you

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

aerrasti created an issue. See original summary.

aerrasti’s picture

FileSize
2.88 KB

Sorry,

I think that I mixed different ideas.

I was already using another patch for the paragraph issue : https://www.drupal.org/files/issues/better_formats_paragraphs-2754029-15...

So I updated the patch checking a new condition.

aerrasti’s picture

I reupload the file with a proper name

mandclu’s picture

Status: Active » Needs review

Not sure if this is still a problem, but checking that a variable contains the expected object before attempting to use its meant god seems sensible enough.

renatog’s picture

Status: Needs review » Needs work
+++ b/better_formats.module
@@ -10,6 +10,25 @@ use Drupal\Core\Form\FormStateInterface;
+function better_formats_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
+  $field_definition = $context['items']->getFieldDefinition();
+  if (better_formats_is_text_format($field_definition->getType())) {
+    // Since $form object cannot be trusted to retrieve $entity and $field_name
+    // in better_formats_filter_process_format when using embed entities
+    // (with paragraphs, or inline_entity_form), let's handle it in field_widget_alter,
+    // where we can work with $context.
+    $element['#entity'] = $context['items']->getEntity();
+    $element['#field_name'] = $context['items']->getName();
+  }
+}

I'd say that we can improve this logic with early return

renatog’s picture

The patch needs to be updated as well because it was implementing a hook_field_widget_form_alter but currently already have this hook at 8.x-1.x https://git.drupalcode.org/project/better_formats/-/blob/8.x-1.x/better_...