function layout_builder_post_update_make_layout_untranslatable

Set the layout builder field as non-translatable where possible.

File

core/modules/layout_builder/layout_builder.post_update.php, line 203

Code

function layout_builder_post_update_make_layout_untranslatable() {
    
    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
    $field_manager = \Drupal::service('entity_field.manager');
    $field_map = $field_manager->getFieldMap();
    foreach ($field_map as $entity_type_id => $field_infos) {
        if (isset($field_infos[OverridesSectionStorage::FIELD_NAME]['bundles'])) {
            $non_translatable_bundle_count = 0;
            foreach ($field_infos[OverridesSectionStorage::FIELD_NAME]['bundles'] as $bundle) {
                // The field map can contain stale information. If the field does not
                // exist, ignore it. The field map will be rebuilt when the cache is
                // cleared at the end of the update process.
                if (!($field_config = FieldConfig::loadByName($entity_type_id, $bundle, OverridesSectionStorage::FIELD_NAME))) {
                    continue;
                }
                if (!$field_config->isTranslatable()) {
                    $non_translatable_bundle_count++;
                    // The layout field is already configured to be non-translatable so it
                    // does not need to be updated.
                    continue;
                }
                if (_layout_builder_bundle_has_no_translations($entity_type_id, $bundle) || _layout_builder_bundle_has_no_layouts($entity_type_id, $bundle)) {
                    // Either none of the entities have layouts or none of them have
                    // translations. In either case it is safe to set the field to be
                    // non-translatable.
                    $field_config->setTranslatable(FALSE);
                    $field_config->save();
                    $non_translatable_bundle_count++;
                }
            }
            // Set the field storage to untranslatable if the field config for each
            // bundle is now untranslatable. This removes layout fields for the
            // entity type from the Content Translation configuration form.
            if (count($field_infos[OverridesSectionStorage::FIELD_NAME]['bundles']) === $non_translatable_bundle_count) {
                $field_storage = FieldStorageConfig::loadByName($entity_type_id, OverridesSectionStorage::FIELD_NAME);
                $field_storage->setTranslatable(FALSE);
                $field_storage->save();
            }
        }
    }
}

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