0

I am using Drupal 8.9.8 and I have created a custom content type with field types such as plain text, number (integer), and so on. Then, I created Content based on the custom content types and filled in the field values. This generated a /node/2 page.

I want to combine the field values from the custom content type and generate a URL link that will be displayed in the /node/2.

The generated URL will be in the following format based on the content type field values which are set as default values of the below:

field_url = https:/www.example.com?SIG=

field_id = 94ddkf03la1

https:/www.example.com?SIG=94ddkf03la1

I have created successfully a module that generates a URL link with .php and .yml accessible from localhost/drupal8/modulename.

I want to accomplish the same functionality, but fetching the values from the custom content type fields.

Where this source code should be placed? Inside modulename.module?

Here is a screenshot of the structure of the custom content type.

enter image description here

I search through the Internet and StackExchange, but I could not figure out or combine the solutions presented.

Please forgive me if the terminology is misleading as I am new to this and I am trying to learn.

3
  • Where do you want to display this link? Because if it is as part of the content display you can use hook_entity_view_alter but if you want be configurable by display you can use extra fields, or maybe you want to use it in views. Depending on the context, the answer can call far from one point to other.
    – d70rr3s
    May 19, 2022 at 10:24
  • @d70rr3s I want the link to be generated in /node/2 page. Care to elaborate more on the implementation of this as I am new and still learning. Thanks! May 19, 2022 at 10:38
  • Drupal 8 reached end of life in November 2021. Even the newest version of 8, which is not 8.9.8, is missing security updates.
    – cilefen
    May 19, 2022 at 11:00

1 Answer 1

0

Sorry for the late response but, haven't able to find myself some time to elaborate a good sample that may serve you as copy/paste answer.

Said that, first of all you need to register your extra field. Since you are trying to generate a link I think generated_link is a good name for this extra field.

/**
 * Implements hook_entity_extra_field_info().
 */
function mymodule_entity_extra_field_info() {
  return [
    // the entity-type you want the field, assuming node.
    'node' => [
      // the node-type you want the field, put your actual bundle here.
      'mybundle' => [
        // where the field should appear, can be form or display.
        'display' => [
          // the actual field.
          'generated_link' => [
            'label' => t('Generated link'),
            'description' => t('Add some comment here to describe your field.'),
            'weight' => 100,
            'visible' => TRUE,
          ],
        ],
      ],
    ],
  ];
}

This only places the extra field on the entity view display, now you need to provide the actual output for the field.

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function mymodule_node_view(array &$build, 
  \Drupal\Core\Entity\EntityInterface $entity, 
  \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, 
  $view_mode) {
  // only do the extra work if the component is configured to be displayed.
  // this assumes a 'generated_link' extra field has been defined for the
  // entity bundle in hook_entity_extra_field_info().
  if ($entity->bundle() === 'mybundle' 
    && $display->getComponent('generated_link')) {
    $build['generated_link'] = [
      // you need to implement the mymodule_generated_link_uri function to build the URI there.
      '#url' => \Drupal\Core\Url::fromUri(mymodule_generated_link_uri($entity)),
      '#type' => 'link',
    ];
  }
}

At this point, clear the cache, go to your node bundle view display setting and you will see the extra field there. Just drag it around to put it where you want it, and go to the node page to see it working.

Other way to get this done, is by using either Display Suite module with it's markup fields or the EFS module, both provide a no-code, full UI solution but you need to rely on tokens to get it done.

Finally, as @cilefen said on comments, using Drupal 8 at this time is not recommended and you should upgrade as soon as possible, since the longer you take to upgrade, more painful and effort consuming will be. Also on Drupal 9.3+ you can leverage the bundle classes feature, which is perfect for this use case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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