0

I want to add custom button on node page.

enter image description here

I tried with hook_node_view but it didn't show up.

like this:

function mymodule_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

$build['mybutton'] = [
  '#type'       => 'button',
  '#name'       => 'mybutton_name',
  '#value'      => t('Click Here'),
  '#attributes' => [ 'class' => 'button button--primary'],
  '#weight'     => -11,
];

}

What am I doing wrong?

EDIT:

Here is the template

{#
/**
 * @file
 * Display Suite 2 column stacked template.
 *
 * Available variables:
 * - outer_wrapper: outer wrapper element
 * - header_wrapper: wrapper element around header region
 * - left_wrapper: wrapper element around left region
 * - right_wrapper: wrapper element around right region
 * - footer_wrapper: wrapper element around footer region
 * - attributes: layout attributes
 * - header_attributes: attributes for header region
 * - left_attributes: attributes for left region
 * - right_attributes: attributes for right region
 * - footer_attributes: attributes for footer region
 * - header: content of header region
 * - left: content of left region
 * - right: content of right region
 * - footer: content of footer region
 */
#}

<{{ outer_wrapper }}{{ attributes.addClass('ds-2col-stacked', 'clearfix') }}>

  {{ title_suffix.contextual_links }}

  <{{ header_wrapper }}{{ header_attributes.addClass('group-header') }}>
    {{ header }}
  </{{ header_wrapper }}>

  <{{ left_wrapper }}{{ left_attributes.addClass('group-left') }}>
    {{ left }}
  </{{ left_wrapper }}>

  <{{ right_wrapper }}{{ right_attributes.addClass('group-right') }}>
    {{ right }}
  </{{ right_wrapper }}>

  <{{ footer_wrapper }}{{ footer_attributes.addClass('group-footer') }}>
    {{ footer }}
  </{{ footer_wrapper }}>

</{{ outer_wrapper }}>
  • And did you clear cache? – Jonh Aug 9 at 9:36
  • 1
    Ds use hook_entity_view_alter, this hook will run after hook_entity_views. You can using hook_entity_view_alter with your name module start with character after D (example e,g, h...) and copy your code to inside. – Jonh Aug 9 at 10:04
  • 1
    Because the same hook execute by order, default same hook will order by module name. You need a hook run after ds_entity_view_alter() so module name start with ... – Jonh Aug 9 at 11:27
  • 1
    I tried what you said but same results then disabled ds, only I can view my button. As it seems I need to add this button somehow on template file but don't know exactly how? – tasqyn Aug 9 at 12:38
  • 1
    You need print it {{ mybutton }} in template twig – vinhdv Aug 14 at 7:28
0

Just tried it out myself and for me it didn't work using your malformatted #attributes array. After I fixed it the following did work. class must provide another array.

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function MYMODULE_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

  $build['mybutton'] = [
    '#type'       => 'button',
    '#name'       => 'mybutton_name',
    '#value'      => t('Click Here'),
    '#attributes' => ['class' => ['button', 'button--primary']],
    '#weight'     => -11,
  ];
}
  • 1
    You are right but still not showing up, seems problem with display suite as @leymannx wrote – tasqyn Aug 9 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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