Add ability to modify the embed options

Created on 19 May 2022, almost 3 years ago
Updated 19 October 2023, over 1 year ago

Problem/Motivation

I want to limit the number of tweets that are rendered when adding a twitter timeline embed.

I have been researching and the oEmbed twitter implementation allow to provide a limit parameter to achieve this. However, there is not any way to pass this parameter to the embed https://github.com/oscarotero/Embed library (used by drupal/embed).

Proposed resolution

Implement a hook_url_embed_options_alter that is called just before the \Embed\Embed object is created and allow to modify the passed configuration.

Then, we would be able to accomplish this behavior by doing something like:

function my_module_url_embed_options_alter($url, &$config) {
  $parsedUrl = parse_url($url);

  if (($parsedUrl['host'] ?? '') == 'twitter.com') {
    $config = array_merge($config, ['oembed' => ['parameters' => ['limit' => 4]]]);
  }
}

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain unstatu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    5 pass
  • πŸ‡ͺπŸ‡ΈSpain tonibarbera

    Here a patch for the url_embed version 2.0.0-alpha1

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    5 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    5 pass
  • Status changed to Needs work over 1 year ago
  • πŸ‡¨πŸ‡­Switzerland berdir Switzerland

    Open to that, but this should use a ->alter(), not invokeAll() and the hook needs to be documented.

  • πŸ‡ͺπŸ‡ΈSpain unstatu

    Thanks @Berdir. I have updated the patch to use the ->alter() method. Good point.

    Where should I document the hook usage? In the readme file?

  • πŸ‡ͺπŸ‡ΈSpain unstatu

    Forgot to add the patch.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Where should I document the hook usage? In the readme file?

    I've added a documentation guide for this module, and added a page dedicated to modifying and extending the code:

    https://www.drupal.org/docs/extending-drupal/contributed-modules/contrib... β†’

    Let's add documentation of this alter hook there.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    I've updated documentation at https://www.drupal.org/docs/extending-drupal/contributed-modules/contrib... β†’

    However, per https://www.drupal.org/docs/develop/standards/php/api-documentation-and-... β†’ , this should also be documented in an url_embed.api.php file.

    Leaving this as "Needs work" for that code change.

  • Status changed to Needs review 11 months ago
  • πŸ‡ͺπŸ‡ΈSpain unstatu

    Thanks @mark_fullmer for the feedback and for writting the documentation. I have added the url_embed.api.php file with the documentation.

  • Status changed to Fixed 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • Status changed to Needs work 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Okay, great! I think this now should be set to merge into the 3.x branch, rather than 8.x-1.x. I've changed that on the merge request. Can you resolve the merge conflict?

  • First commit to issue fork.
  • Merge request !23Resolve #3281357 "Alter hook 3.x" β†’ (Open) created by mortona2k
  • Pipeline finished with Failed
    4 months ago
    Total: 242s
    #382939
  • πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

    Rebased onto 3.x.

    Included dependency injection for ModuleHandler to assist with https://www.drupal.org/project/url_embed/issues/3488449 πŸ“Œ Replace \Drupal calls with dependency injection Active

    This hook is included in the documentation, so there should either be a link back to this issue for the patch or we should provide a release with it included.

    --

    As an aside, when I did the rebase, git got hung up on one of the commit messages starting with #, IE #3281357 for the commit number.

    I have no idea why that happened, and for only one of the commits, but I had to remove the # to get it to register the commit message.

    There was a conflict on this commit, and it opened in vim for editing. I'm not sure if one of those has to do with reading a line starting with # as an empty line. If so (maybe this is related to editing the message for a conflicting commit), how would I accept the existing message starting with #?

    (You can see the last commit in this list doesn't start with #: https://git.drupalcode.org/project/url_embed/-/merge_requests/23/commits)

  • πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

    The hook works, but doesn't do what I as looking for.

    Maybe this is a misunderstanding on my part, but I was thinking that this would allow modifying the oembed response.

    In the case for Codepen, the response is: https://codepen.io/api/oembed?format=json&url=https://codepen.io/mortona...

    I was thinking this would allow modifying the attributes on the iframe, for example.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Maybe this is a misunderstanding on my part, but I was thinking that this would allow modifying the oembed response.

    Would this alter hook achieve what you wanted if $url was also pass-by-reference, as in:

    $this->moduleHandler->invokeAll('url_embed_options_alter', [&$url, &$config]);
    
  • πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

    That would let me change the url entered by the user.

    I was looking at modifying the iframe response:

    <iframe id="cp_embed_ZVYeqL" src="https://codepen.io/mortona42/embed/preview/ZVYeqL?default-tabs=js%2Cresult&amp;height=900&amp;host=https%3A%2F%2Fcodepen.io&amp;slug-hash=ZVYeqL" title="Calendar" scrolling="no" frameborder="0" height="900" allowtransparency="true" class="cp_embed_iframe" style="width: 100%; overflow: hidden;"></iframe>

    I am trying to debug an issue where I should be able to use height=900 in the url, and that is used for rendering. You can see it above, but that's from going directly to the url endpoint.

    What I was actually trying to modify is the $embed, after $embed->get($url).

    When I inspect this, I see the api response in oembed->data, but it doesn't have my height setting.

  • πŸ‡ΊπŸ‡ΈUnited States mortona2k Seattle

    What I'm looking for may be an extension of this issue.

    Currently, we can only enter a url, and it will just display the response.

    I would like it to show a form to edit the attribute values or html snippet before it is inserted.

    Maybe a plugin to define the form and changes to the response.

    I tried Embedded Content β†’ , which allows you to do that by defining a plugin. I'm able to customize a form for the codepen embed options, and it works pretty well. That module is not oembed specific, but my plugin uses the core services for it.

    Posted the plugin/codepen support here: https://www.drupal.org/project/codepen/issues/3496793 ✨ Support WYSIWYG embedding Active

  • Status changed to Needs review about 2 months ago
  • First commit to issue fork.
  • πŸ‡ͺπŸ‡ΈSpain akalam

    I'm adding a commit allowing to alter the url, as suggested at #18, as I'm having also the same need

  • Pipeline finished with Failed
    about 2 months ago
    Total: 324s
    #442676
  • First commit to issue fork.
  • Pipeline finished with Success
    about 2 months ago
    Total: 175s
    #444538
  • πŸ‡ͺπŸ‡ΈSpain xito

    I get the following error after applying MR changes

    Deprecated: Optional parameter $config declared before required parameter $module_handler is implicitly treated as a required parameter in /var/www/html/web/modules/contrib/url_embed/src/UrlEmbed.php on line 42

    Code modified on the issue fork and generated the patch

Production build 0.71.5 2024