diff --git a/addtoany.module b/addtoany.module index 481c577..41ec743 100644 --- a/addtoany.module +++ b/addtoany.module @@ -13,6 +13,7 @@ use Drupal\Core\Url; use Drupal\Component\Utility\UrlHelper; use Drupal\node\Entity\Node; use Drupal\addtoany\Form\AddToAnySettingsForm; +use Drupal\Core\Entity\ContentEntityInterface; /** * Implements hook_help(). @@ -236,20 +237,27 @@ function addtoany_page_attachments(&$page) { } /** - * Generate data for AddToAny buttons for a node. + * Generate data for AddToAny buttons for an entity. * - * @param object $node - * The node object to create the buttons for. + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * The entity object to create the buttons for. * @param object $config * If present this will be used as custom config data. Use NULL for default * config data. * * @return array - * The array with url, title, additional_html that will be send to Twig. + * The array with url, title, additional_html that will be sent to Twig. */ -function addtoany_create_entity_data($node, $config = NULL) { - $url = isset($node) ? $node->toUrl('canonical', ['absolute' => TRUE])->toString() : NULL; - $title = isset($node) && $node->access('view label') ? $node->label() : NULL; +function addtoany_create_entity_data(ContentEntityInterface $entity, $config = NULL) { + // If the entity is Paragraph, find the first parent + // that is Node. + while ($entity instanceof EntityInterface && $entity->getEntityTypeId() == 'paragraph') { + $entity = $entity->getParentEntity(); + } + + $url = $entity->toUrl('canonical', ['absolute' => TRUE])->toString() ?? NULL; + $title = $entity->label() ?? NULL; + return addtoany_create_data($url, $title, $config); }