8 – How to get parent paragraph from a child block preprocess

Searched all over the place but cannot find anything similar. Here’s the issue…

The setup:

  • ‘Page’ content type
  • ‘Block Content’ paragraph type

The ‘Page’ content type has a field for referencing the ‘Block Content’ paragraph, called “Components” (set to unlimited). Now we can use paragraphs within our page. Simple.

The ‘Block Content’ paragraph type has a field for referencing blocks (using the module Block Field). It also has two other fields for title and subtitle. These fields need to be displayed on the referenced block template for this paragraph instance. This is the issue.

I have included some images to show the setup.

I am trying to use theme_preprocess_block to inject these fields (title and subtitle) into the correct block using $vars(‘plugin_id’) from the block preprocess but the issue is that the block does not know its paragraph parent. And if I don’t know the parent, I can’t retrieve these fields that I need to inject into the block.

This answer suggests using the following to get the parent:

$parent   = $variables('elements')('#node')->_referringItem->getEntity();

But in my case I could not find anything equivalent in my block preprocess function.

Update 1:

I tried (from within theme_preprocess_block):

dpm($vars('elements')('content')('#block_content')->_referringItem->getEntity());

This give the following error:

Error: Call to a member function getEntity() on null in limit_preprocess_block__banner_block()

Update 2:

I just dumped the $vars from the preprocess function and noticed that there seems to be the paragraph id in there. But I don’t know how to access it from the $vars variable. Is it possible? (see dump image below)

enter image description here

IMAGE 1:

enter image description here


IMAGE 2:

enter image description here


IMAGE 3:

enter image description here


IMAGE 4:

enter image description here


IMAGE 5:

enter image description here

Get Title of a referenced Node from a field within a paragraph in Views, Drupal 9?

I am trying to build a View that lists People’s Profiles which are referenced to a Team via a Paragraph (within that Profile) to print out the Name of the Person and other data from the Profile.

Content-Type “Team” is for representing a Team, with Title, Picture and Description.

Content-Type “Profile” has some Fields like Title, Name, … and a Paragraph with further Role-based Data for a Team or some Teams.

Profile:

  • Name
  • Body
  • Role_in_Team (Paragraph):
    • Datetime_Start
    • Datetime_End
    • Role_Name
    • Team_Reference (Entity reference to a Team)

On a Team’s page, a View shall show the matching Profiles beneath.

I build a Views Block with:

  • Contextual Filter with “ID from URL” to relate everything just to the shown Team page
  • A Relationship with “Team_Reference” to show only Paragraphs that are referenced to (this) Team (Paragraph uses “Team_Reference”)
  • A Relationship with “Content using Team_Reference” (with Relationship “Team_Reference” within)

The View Block shows the correct Number of Profile Entries (that are referenced) but I can’t get it to show the Profile’s Names.

How to get Access to the Fields of the Parent Node of the specific Paragraph?

Sample-Output of Views Block

I add the Export of this Views Block here…

uuid: 60312c0b-a7b5-4e59-9935-005b4eeec9c3
langcode: de
status: true
dependencies:
  config:
    - field.storage.node.field_geburtstag
    - field.storage.paragraph.field_bezeichnung
  module:
    - datetime
    - node
    - paragraphs
    - user
    - views_bootstrap
id: team_profile
label: Team->Profile
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
  default:
    display_plugin: default
    id: default
    display_title: Master
    position: 0
    display_options:
      access:
        type: perm
        options:
          perm: 'access content'
      cache:
        type: tag
        options: {  }
      query:
        type: views_query
        options:
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_comment: ''
          query_tags: {  }
      exposed_form:
        type: basic
        options:
          submit_button: Anwenden
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      pager:
        type: some
        options:
          items_per_page: 5
          offset: 0
      style:
        type: views_bootstrap_table
        options:
          grouping: {  }
          row_class: ''
          default_row_class: true
          override: 1
          sticky: 0
          caption: ''
          summary: ''
          description: ''
          columns:
            title: title
            field_geburtstag: field_geburtstag
          info:
            title:
              sortable: 0
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: 0
              responsive: ''
            field_geburtstag:
              sortable: 0
              default_sort_order: asc
              align: ''
              separator: ''
              empty_column: 0
              responsive: ''
          default: '-1'
          empty_table: 0
          responsive: 0
          bootstrap_styles:
            striped: 0
            bordered: 0
            hover: 0
            condensed: 0
      row:
        type: fields
      fields:
        title:
          id: title
          table: node_field_data
          field: title
          relationship: none
          group_type: group
          admin_label: ''
          label: ''
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings:
            link_to_entity: true
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
        field_geburtstag:
          id: field_geburtstag
          table: node__field_geburtstag
          field: field_geburtstag
          relationship: none
          group_type: group
          admin_label: ''
          label: ''
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: datetime_default
          settings:
            timezone_override: ''
            format_type: medium
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
        field_bezeichnung:
          id: field_bezeichnung
          table: paragraph__field_bezeichnung
          field: field_bezeichnung
          relationship: reverse__paragraph__field_referenz_auf_team
          group_type: group
          admin_label: ''
          label: Bezeichnung
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings: {  }
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
      filters:
        status:
          value: '1'
          table: node_field_data
          field: status
          plugin_id: boolean
          entity_type: node
          entity_field: status
          id: status
          expose:
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
          group: 1
      sorts:
        created:
          id: created
          table: node_field_data
          field: created
          order: DESC
          entity_type: node
          entity_field: created
          plugin_id: date
          relationship: none
          group_type: group
          admin_label: ''
          exposed: false
          expose:
            label: ''
          granularity: second
      title: Team->Profile
      header: {  }
      footer: {  }
      empty: {  }
      relationships:
        reverse__paragraph__field_referenz_auf_team:
          id: reverse__paragraph__field_referenz_auf_team
          table: node_field_data
          field: reverse__paragraph__field_referenz_auf_team
          relationship: none
          group_type: group
          admin_label: field_referenz_auf_team
          required: false
          entity_type: node
          plugin_id: entity_reverse
        field_referenz_auf_team:
          id: field_referenz_auf_team
          table: paragraph__field_referenz_auf_team
          field: field_referenz_auf_team
          relationship: reverse__paragraph__field_referenz_auf_team
          group_type: group
          admin_label: 'field_referenz_auf_team: Inhalt'
          required: false
          plugin_id: standard
      arguments:
        nid:
          id: nid
          table: node_field_data
          field: nid
          relationship: none
          group_type: group
          admin_label: ''
          default_action: default
          exception:
            value: all
            title_enable: false
            title: All
          title_enable: false
          title: ''
          default_argument_type: node
          default_argument_options: {  }
          default_argument_skip_url: false
          summary_options:
            base_path: ''
            count: true
            items_per_page: 25
            override: false
          summary:
            sort_order: asc
            number_of_records: 0
            format: default_summary
          specify_validation: false
          validate:
            type: none
            fail: 'not found'
          validate_options: {  }
          break_phrase: false
          not: false
          entity_type: node
          entity_field: nid
          plugin_id: node_nid
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:field.storage.node.field_geburtstag'
        - 'config:field.storage.paragraph.field_bezeichnung'
  block_1:
    display_plugin: block
    id: block_1
    display_title: Block
    position: 1
    display_options:
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:field.storage.node.field_geburtstag'
        - 'config:field.storage.paragraph.field_bezeichnung'

How to get the URL of remote video inserted by media field in block or paragraph twig?

How to get the URL of remote video inserted by media field in block or paragraph twig?
I am using the paragraphs module to create video galleries from youtube videos. When I use {{ content.video_gallery }} it’s working and showing the youtube video thumbnail but I want to show this video in a popup for which I have written a code in which I need only the URL of the youtube video.
I am searching for two days I found solutions for image and video but no solution found for remote video URL.

How to aggregate values from fields in Paragraphs based on value of another field in that same paragraph, and display in an aggregated Views table?

Hopefully my title makes sense…

I am setting up a Drupal 9 website for internal use as a financial reporting engine and a CRM. I am making use of the following modules:

  • Paragraphs
  • Views
  • Viewfield
  • View Fields View
  • Views Aggregator Plus

I have one Paragraph (Entity Reference) unlimited-value field on a paragraph type called “Project”. That field is called “Transactions”. It refers to a paragraph type called “Transaction”.

The “Transaction” paragraph type has the following fields:

  • Transaction Amount (decimal field))
  • Status (term reference field. Values are PAID and UNPAID)
  • Paid On (date field)
  • Year (a taxonomy entity reference field that contains pre-set years (2019, 2020, 2021, etc).

I have a content type called “Company”. It has the following fields:

  • Title
  • Project (a Paragraph/Entity Reference field)
  • Transaction Amount – Paid (a Viewfield)
  • Transaction Amount – Unpaid (a Viewfield)
    • These two Viewfields are of content type “Company”
      • One is filtered to the Transaction paragraph’s status “Paid”; the other is filtered to “Unpaid”.
    • They contain this field, which is SUMmed through Views Aggregation: (field_transactions: Paragraph) SUM(Paragraph: Transaction Amount)
    • They contain these relationships:
    field_projects_paragraph: Paragraph
    (field_projects_paragraph: Paragraph) field_transactions: Paragraph
    (field_projects_paragraph: Paragraph) field_transactions




    

The output displays the sum of the paid or unpaid transactions. For example, if a company has a project which has 3 PAID transactions of $100 each, this view will output “$300”. The intent is to display this value on the “Overview of Transactions” view, as described below:

Overview of Transactions view…

I have created a View of “Company” content of display format: “Table with Aggregation Options” that will display a table of the following columns:

  • Company Title
  • Total Transaction Amount (a SUM aggregation of all transaction “Transaction Amount” values.
  • Total Transaction Amount with status = PAID
  • Total Transaction Amount with status = UNPAID

The intent is to display an at-a-glance table of all Companies on the site, showing a line for each Company, that Company’s Total Transaction amount (which includes paid and unpaid), that company’s PAID total amount, and that company’s UNPAID total amount.

Further, the intent is to be able to filter this table based on the “Year” taxonomy term value from the Transaction paragraph type (filter it to only contain data that is tagged with “2020”, for example), or to filter it by date range using the “Paid On” value from the Transaction paragraph type (filter it to only contain data that contains a date value of 01/01/2020 – 01/03/2020, for example). The purpose is to be able to see all dollar amounts per company on a yearly, monthly, weekly, daily, or any other date range basis. For example: “display all company transaction data for Jan 1, 2020 through March 31, 20020, and then run that same report for that same time period in 2021.

To the View, I added the following settings:

Relationships:

    - field_projects_paragraph: Paragraph
    - (field_projects_paragraph: Paragraph) field_transactions: Paragraph

Filter Criteria:

    Content: Content type (= Company)
    (field_transactions: Paragraph) Paragraph: Year (exposed)
    (field_transactions: Paragraph) Paragraph: Paid On (exposed)

Fields:

    Content: ID (ID) (hidden)
    (field_transactions: Paragraph) Paragraph: ID (ID) (hidden)
    Content: Title (Title)
    (field_transactions: Paragraph) SUM(Paragraph: Transaction Amount) (Transaction Amount)
    Content: Transaction Amount - Paid (Transaction Amount - Paid) - This is one of the Views Field View fields from the Company content type
    Content: Transaction Amount - Unpaid (Transaction Amount - Unpaid) - This is one of the Views Field View fields from the Company content type

Format:Table with aggregation options

        ID: Apply Group Function: Group and Compress
        (field_transactions: Paragraph) Transaction Amount: Column Aggregation: Sum

    

My current end result is as follows:

  • Each Company has its only single line in the table.
  • The “Total
    Transaction Amount” field correctly sums the company’s total
    transactions (paid and unpaid), and the column successfully
    aggregated the total transactions of all Companies on the site.
  • The
    “Content: Transaction Amount – Paid (Transaction Amount – Paid)”
    field correctly displays the company’s total PAID transactions, but
    it does not adhere to the Year and Paid On filters. Additionally, the
    column will not SUM with the Table Aggregation settings at all.
  • The
    “Content: Transaction Amount – Unpaid (Transaction Amount – Paid)”
    field correctly displays the company’s total UNPAID transactions, but
    it does not adhere to the Year and Paid On filters. Additionally, the
    column will not SUM with the Table Aggregation settings at all.

How can I display each company’s Paid and Unpaid transaction totals in this Views aggregated table, and also make those two values subject to the Year and Paid On filters?

I am open to completely re-thinking the organization of the node types, data, etc.

java – Separate sentences in paragraph that doesn’t have punctuation

How can I separate sentences in paragraph that doesn’t have punctuation?

For example, I have this paragraph:

The red glint of paint sparkled under the sun he had dreamed of
owning this car since he was ten and that dream had become a reality
less than a year ago it was his baby and he spent hours caring for
it pampering it and fondling over it she knew this all too well
and that’s exactly why she had taken a sludge hammer to it

I need the output to be something like this:

The red glint of paint sparkled under the sun. He had dreamed of
owning this car since he was ten and that dream had become a reality
less than a year ago. It was his baby and he spent hours caring for
it pampering it and fondling over it. She knew this all too well
and that’s exactly why she had taken a sludge hammer to it.

I just need to add punctuations to separate the sentences int the paragraph, I don’t need to add commas.

Is there any ideas or keywords that would help me with this problem?

i18n l10n – How to print the paragraph field translation value in twig template

I am trying to access the nested paragraph field inside my twig template.
{% for items in item[‘#paragraph’].get_aw_title_details%}
{{ item.entity.field_award.value }}
{% endfor %}

When I am using this syntax it is always returning the English content. Translation content is not visible for me. For chinese also it is giving the english content

8 – How to access paragraph entity reference that’s inside another paragraph?

I have a Paragraph call sponsor_category using another Entity Reference Revision call sponsor. The Entity Reference Revision sponsor has field_sponsor_image and field_sponsor_link. I was wondering how do I access those fields using Twig?

Inside my `paragraph–sponsor-category.html.twig page, I tried:

{{ paragraph.field_sponsor.entity.field.value }}

and

{% for key, item in content.field_sponsor if key|first != '#'%}
 {{ item['#paragraph'].field_sponsor_image[0].value|raw }}
 {{ item['#paragraph'].field_sponsor_link[0].value|raw }}
{% endfor %}

Thanks!

migration – Migrating paragraph items to their host nodes?

I am trying to do a Drupal 7 -> Drupal 9 migration using the UI. It was able to convert field collection types to paragraph types. I was also able to create a few custom migrations using the d7_paragraphs_item plugin provided by Paragraphs to migrate all paragraph items from D7 into the database.

Now I need to attach them to the nodes.

My migration yaml:

id: field_content
migration_tags: {}
migration_dependencies: {}
migration_group: yale
label: Field Content Paragraphs
source:
  plugin: node_items
  key: migrate
destination:
  plugin: 'entity:node'
process:
  nid:
    - plugin: migration_lookup
      migration: d7_node_complete:page
      source: entity_id
      no_stub: true
    - plugin: extract
      index:
        - 0
  title: title
  items:
    plugin: migration_lookup
    migration:
      - accordion_items_paragraph
      - editor_items
      - highlight_files
    source: field_content_value
  field_content:
    plugin: sub_process
    source:
      - '@items'
    process:
      target_id: '0'
      target_revision_id: '1'

Except this is only doing maybe 20% of the total. It would appear that its not pulling back all possible paragraph items to stick into the field, or maybe I am just thinking of it wrong.

The source plugin is returning a set of data from Drupal 7 for a field that held Paragraph references:

class NodeItems extends SqlBase {

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('field_data_field_content', 'fc');
    $query->join('node', 'n', 'n.nid = fc.entity_id');
    $query->join('paragraphs_item', 'pi', 'pi.item_id = fc.field_content_value');
    $query->fields('fc', (
        'entity_id',
        'bundle',
        'delta',
        'field_content_value'
      ));
    $query->fields('n', (
      'title',
    ));
    $query->condition('n.status', 1);
    $query->condition('fc.bundle', 'accordions', '!=');
    $query->orderBy('fc.entity_id');
    $query->orderBy('fc.delta');
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return (
      'title' => $this->t('title'),
      'entity_id' => $this->t('entity_id'),
      'bundle' => $this->t('bundle'),
      'delta' => $this->t('delta'),
      'field_content_value' => $this->t('field_content_value'),
    );
  }

“field_content” in D9 is a Paragraphs field. It does seem to plug some values, I do see items on various nodes. But its failing because I think the lookup doesn’t always return a value or maybe it doesn’t know what to do with them? Also the nid: mapping needs to support multiple node types, I passed one migration. There are 12. Can I add all of them there or ?

Is this the way to attach them to host nodes? Is there a better way?