9 – Is there a way to make a single type Paragraphs field not required?

My Content Type has a Paragraphs field that is Not required. The Paragraph type has a required field for when it is added, but until it is added, this shouldn’t trigger “required” for my field. Since my field only has 1 paragraph type option, it is ALWAYS added! I can Remove the paragraph from the field, and required doesn’t get triggered. But every time the form loads to add a new node, it always adds a paragraph.

e.g.

  • Paragraph type: Image with caption has an image as a required field.
  • Content type has a field: Photo of that paragraph type that is not required.
  • Add content shows paragraph automatically added and required.

Is this a bug, or a setting I’m missing?

theming – How can I assign custom field layouts using paragraphs?

For reference:

-modules
--contrib
---ds
--custom
---ds

I’m looking to add paragraph layouts using flexbox. I don’t see a module specifically for this, so I realize I’m going to have to adapt an existing module.

Right now, I’m trying to add custom layouts in the Display Suite module by adding a template config to ds.layouts.twig with the appropriate html.twig and css files.

My concern is that any edits to these files will be overwritten with updates to the DS module. I tried adding a ds folder in the custom modules folder copying over the ds.layouts.twig file and the appropriate files, but it didn’t work as I thought it would.

Does anyone have any recommendations to add layout functionality to paragraphs in which I can write my own layouts using grid/flexbox

9 – Editing paragraphs field settings returns 404 with php development server

I have this strange behaviour on my local drupal9 instance. I start it in the web folder with the build in php development server: php -S localhost:8000

Now if I try to edit any paragraphs field, eg. /admin/structure/paragraphs_type/manual_news/fields/paragraph.manual_news.field_news_entries

[Fri Aug 13 12:47:39 2021] 127.0.0.1:55544 [404]: POST /admin/structure/paragraphs_type/manual_news/fields/paragraph.manual_news.field_news_entries - No such file or directory

I get a 404 from the server. If it runs on a real webserver, it works.

Anybody knows how to debug / fix this?

I also opened https://www.drupal.org/project/paragraphs/issues/3228039 for this issue.

8 – Add ThirdPartySettings to individual paragraphs of a certain bundle?

I want to add configuration settings to Paragraph entities in a specific bundle in Drupal 8. Theres potentially a lot of options and the need to change/update them over time, so I figure the best way is through ThirdPartySettings and not adding more fields to it. I am going to use these values to influence certain aspects of the render and for search queries.

I can see where you can add settings to the bundle itself, but how do you provide them for each paragraph instance on a node via a config schema?

I see this in the Paragraphs config schema dir:

paragraphs.paragraphs_type.*:
  type: config_entity
  label: 'Paragraphs type config'
  mapping:
    .....

But paragraphs.paragraphs_type.*.third_party.foo... would add it to the bundle, right?

8 – Migration of CSV into paragraphs fails with unexpected error

I try to import CSV files into a Drupal 8 installation but I can’t manage to make the relationship to paragraphs appear. I followed the steps of this tutorial but can’t figure out how to make it work. I created a simple test case to find the error:

I extended the regular article content type with a paragraphs field called field_persons for linking each article to a list of persons with roles. This paragraphs entity has to fields called field_name (for now only text) and field_role (taxonomy_term).

The yaml file for paragraphs migration looks like that and import processes without error:

id: article_persons
label: Import persons for articles

source:
  plugin: csv
  delimiter: '|'
  enclosure: '"'
  path: article_persons.csv
  header_row_count: 1
  keys:
    - id
  column_names:
    0:
      id: 'ID'
    1:
      name: 'Name'
    2:
      rol: 'Role'

process:
  field_nombre: name
  field_para_rol: 
    plugin: entity_generate
    source: role
    value_key: name
    bundle_key: vid
    bundle: rol
    entity_type: taxonomy_term
    ignore_case: true

destination:
  plugin: 'entity_reference_revisions:paragraph'
  default_bundle: article_persons

The yaml file for article migration looks like that and import processes without error:

id: article
label: Import articles

source:
  plugin: csv
  delimiter: '|'
  enclosure: '"'
  path: articles.csv
  header_row_count: 1
  keys:
    - id
  column_names:
    0:
      id: 'ID'
    1:
      title: 'Title'
    2:
      person: 'Persons'

process:
  title: title
  type:
    plugin: default_value
    default_value: article
  field_person/target_id: 
    -
      plugin: migration_lookup
      migration: article_persons
      no_stub: true
      source: id
    -
      plugin: extract
      index:
        - '0'
  field_person/target_revision_id: 
    -
      plugin: migration_lookup
      migration: article_persons
      no_stub: true
      source: id
    -
      plugin: extract
      index:
        - 1

destination:
  plugin: 'entity:node'
migration_dependencies:
  required: 
    - article_persons
  optional: { }

But when I try to access the node I get an “unexpected error” with a huge log message:

Error: Call to a member function getEnabledBehaviorPlugins() on null en DrupalparagraphsParagraphViewBuilder->buildMultiple() (lĂ­nea 37 de /var/www/html/sites/all/modules/paragraphs/src/ParagraphViewBuilder.php)
#0 /var/www/html/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(220): DrupalparagraphsParagraphViewBuilder->buildMultiple(Array)
#1 (internal function): DrupalCoreEntityEntityViewBuilder->build(Array)
#2 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(378): call_user_func(Array, Array)
#3 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(195): DrupalCoreRenderRenderer->doRender(Array, false)
#4 /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php(490): DrupalCoreRenderRenderer->render(Array)
#5 /var/www/html/sites/default/files/php/twig/5a008e9488529_field.html.twig_9MJB5waMfG4Lcal_NDL6EY6rM/NRByUw7bYmwhNIE9WTZfCRjJ3XUAFBB122RdQTC3JsI.php(128): DrupalCoreTemplateTwigExtension->escapeFilter(Object(DrupalCoreTemplateTwigEnvironment), Array, 'html', NULL, true)
#6 /var/www/html/vendor/twig/twig/lib/Twig/Template.php(432): __TwigTemplate_85d8aba47ed6496a0f2e8e6407c9fc1802c03e707a72d9d0c1465c62e125a308->doDisplay(Array, Array)
#7 /var/www/html/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling(Array, Array)
#8 /var/www/html/vendor/twig/twig/lib/Twig/Template.php(411): Twig_Template->display(Array)
#9 /var/www/html/core/themes/engines/twig/twig.engine(64): Twig_Template->render(Array)
#10 /var/www/html/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('sites/all/theme...', Array)
#11 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(437): DrupalCoreThemeThemeManager->render('field', Array)
#12 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(450): DrupalCoreRenderRenderer->doRender(Array)
#13 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(195): DrupalCoreRenderRenderer->doRender(Array, false)
#14 /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php(490): DrupalCoreRenderRenderer->render(Array)
#15 /var/www/html/sites/default/files/php/twig/5a008e9488529_node.html.twig_fOU7Z1faeO15YpZRrtd-F9Ksv/yHD4dpGBMlI8SMMqQ5z-QVQGNcCrpCuzsSt1E4ShcEU.php(111): DrupalCoreTemplateTwigExtension->escapeFilter(Object(DrupalCoreTemplateTwigEnvironment), Array, 'html', NULL, true)
#16 /var/www/html/vendor/twig/twig/lib/Twig/Template.php(432): __TwigTemplate_38fa5b6b1f6bdfd56bb4dba5e7d13d0f61126e79c913612089235f94cf12db1f->doDisplay(Array, Array)
#17 /var/www/html/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling(Array, Array)
#18 /var/www/html/vendor/twig/twig/lib/Twig/Template.php(411): Twig_Template->display(Array)
#19 /var/www/html/core/themes/engines/twig/twig.engine(64): Twig_Template->render(Array)
#20 /var/www/html/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('sites/all/theme...', Array)
#21 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(437): DrupalCoreThemeThemeManager->render('node', Array)
#22 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(195): DrupalCoreRenderRenderer->doRender(Array, false)
#23 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(226): DrupalCoreRenderRenderer->render(Array, false)
#24 /var/www/html/core/lib/Drupal/Core/Render/Renderer.php(576): DrupalCoreRenderMainContentHtmlRenderer->DrupalCoreRenderMainContent{closure}()
#25 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(227): DrupalCoreRenderRenderer->executeInRenderContext(Object(DrupalCoreRenderRenderContext), Object(Closure))
#26 /var/www/html/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(117): DrupalCoreRenderMainContentHtmlRenderer->prepare(Array, Object(SymfonyComponentHttpFoundationRequest), Object(DrupalCoreRoutingCurrentRouteMatch))
#27 /var/www/html/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): DrupalCoreRenderMainContentHtmlRenderer->renderResponse(Array, Object(SymfonyComponentHttpFoundationRequest), Object(DrupalCoreRoutingCurrentRouteMatch))
#28 /var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(108): DrupalCoreEventSubscriberMainContentViewSubscriber->onViewRenderArray(Object(SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent), 'kernel.view', Object(DrupalComponentEventDispatcherContainerAwareEventDispatcher))
#29 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(158): DrupalComponentEventDispatcherContainerAwareEventDispatcher->dispatch('kernel.view', Object(SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent))
#30 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): SymfonyComponentHttpKernelHttpKernel->handleRaw(Object(SymfonyComponentHttpFoundationRequest), 1)
#31 /var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): SymfonyComponentHttpKernelHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#32 /var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): DrupalCoreStackMiddlewareSession->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#33 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): DrupalCoreStackMiddlewareKernelPreHandle->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#34 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupalpage_cacheStackMiddlewarePageCache->pass(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#35 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupalpage_cacheStackMiddlewarePageCache->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#36 /var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#37 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): DrupalCoreStackMiddlewareNegotiationMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#38 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(657): StackStackedHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true)
#39 /var/www/html/index.php(19): DrupalCoreDrupalKernel->handle(Object(SymfonyComponentHttpFoundationRequest))
#40 {main}.

This bug report seems to indicate that the paragraphs which is referenced doesn’t exist. So it seems to have to do something with the ids, but I can’t figure out what.

8 – How to display images from the media library on paragraphs that belong to a body-type field

I’m only able to display text from a body field. How do I display images from the media library?

This is what I tried, but it doesn’t work. Thanks in advance!

{% block paragraph %}
  {% autoescape false %}
    {{ paragraph.field_body.value }}
    {% for media in paragraph.field_body  %}
    <img src="https://drupal.stackexchange.com/{{ file_url(media.entity.field_image.entity.uri.value) }}">
    {% endfor %}
  {% endautoescape %}
{% endblock paragraph %}

paragraphs – Add field to a custom page before rendering

I’ve copied some paragraph from another node and I’d like to copy them into a custom page that’s created in a module.

I’ve done the logic to get the paragraph from the initial node. Now I’m stuck, don’t know how to display paragraph the same way that’s done in the initial node.

Tried by the .module file in the preprocess_page and in the controller, but I couldn’t make the rendering of paragraph works

    <?php

use Drupalmy_paragraphServicesParagraphServices;


/**
 * in mymodule.module
 * Implements hook_preprocess_HOOK().
 */

function mymodule_preprocess_page(&$variables)
{
  $route = Drupal::routeMatch()->getRouteName();
  if($route == 'mymodule.select') {
    /** @var ParagraphServices $service */
    $service = Drupal::service('mymodule.paragraphs');
    $defaultParagraph = $service->getHomePageParagraphs(
      (
        ParagraphServices::TYPE1,
        ParagraphServices::TYPE2,
      )
    );
    $variables('page')('#defaultPararaph') = $defaultParagraph;
  }
}

/**
 * in my Controller
 * @return array
 * @throws DrupalComponentPluginExceptionPluginException
 */
public function index() {
  /** @var DrupalCoreBlockBlockPluginInterface $store_detail_block */
  $select_block = $manager->createInstance('block', ());
  $view = $this->getSearchView();
  $view_render = $view->render();
  $form = $this->formBuilder->getForm('DrupalmymoduleFormForm');

  $defaultParagraphs = $this->paragraphServices->getHomePageParagraphs(
    (
      ParagraphServices::TYPE1,
      ParagraphServices::TYPE2,
    )
  );

  return (
    '#theme' => 'store_locator',
    '#view' => $renderer->render($view_render),
    '#form' => $renderer->render($form),
    '#store_detail' => $store_detail_block->build(),
    '#attached' => ('library' => ('mymodule/template')),
    "#paragraphs" => ($defaultParagraphs),
  );
}

But I’ve got this error

User error: “0” is an invalid render array key in DrupalCoreRenderElement::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
DrupalCoreRenderElement::children(Array, 1) (Line: 407)
DrupalCoreRenderRenderer->doRender(Array) (Line: 449)
DrupalCoreRenderRenderer->doRender(Array, ) (Line: 201)
DrupalCoreRenderRenderer->render(Array) (Line: 450)

By displaying like this

{{ paragraphs }}

8 – Custom formatter for paragraphs field

I want to build a custom formatter for a paragraphs field that renders some of the fields of the paragraph. However, I don’t know how to access the single files of the paragraph field.

So far I’ve only written the skeleton of the formatter, but it doesn’t do much, except that I can return a serialised string of all of the entire field, which does include the necessary information, but I only need three particular fields rendered themselves in a nice way (a term reference field with a label, a boolean field as “yes” or “no” and a text field. A Twig template is not an option for this.

The function looks like this:

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = ();

  foreach ($items as $delta => $item) {  
    // Desired fields: field_network_name, field_network_specific_site_code, field_network_verified

    $element($delta) = (
      '#markup' => "Output: " . serialize($item)
    );
  } 

  return $element;
}

I’d need to know how to elegantly access the fields inside $item.

theming – How do I add a over a paragraph’s field, depending on another field of this paragraph?

what i have is a paragraph (from the paragraphs module) which has two fields :

  • field_title (text)
  • field_title_level (list texte)

The field_title_level can have either h2,h3 or h4 as a value.
What i would like to do is to surround the title text with a h2/h3/h4 div.

I tried using templates (paragraph, or field). With the field one, i managed to add a div over the title, but i didn’t manage to access the field_title_level in order to add the correct div. And using the paragraph template, i didn’t manage to add any div over the title.

Any clue? Thanks in advance.