What should and shouldn't go into the API? (embed_widgets.module)

Currently, the API consists of:

  • function embed_widgets_overview() - Should be moved to UI module.
  • function embed_widgets_load($wid) - Loads a widget given a widget ID.
  • function embed_widgets_block_load($module, $delta) - Loads a widget given a module and block delta.
  • function embed_widgets_save($embed_widget) - Saves a widget.

Other things that will likely go into the API:

  • Functions to customize and theme widgets.
  • A function to provide a list of widgets.

What else makes a good API? I've been told, when in doubt, leave it out.

Perhaps a useful function would be one that returns a list of what types of additional widgets/widget modules are enabled. i.e. IFrame, Google Gadgets, JavaScript, etc.

CommentFileSizeAuthor
#2 embed_widgets_API.patch4.95 KBjtsnow

Comments

nedjo’s picture

Good questions. Yes, the sorts of utility functions you're suggesting are indeed good candidates.

We could consider introducing a hook or set of hooks. E.g., hook_embed_widgets() could allow modules to register the sorts of embed widgets they implement. This could allow us to reduce the code in each embed widget type module. Here are some rough ideas:


/**
 * Return available embed widget types.
 */
function embed_widgets_get_types($refresh = FALSE) {
  static $types = array();
  if ($refresh || empty($types)) {
    $types = module_invoke_all('embed_widget_types');
  }
  return $types;
}

/**
 * Implementation of hook_menu().
 */
function embed_widgets_menu() {
  foreach (embed_widget_types_get_types(TRUE) as $type => $embed_widget) {
    // Add menu items, using the data returned.
    $items['embed-widgets/%embed_widgets/code/' . $type] = array(
      'title' => $embed_widget['title'],
      'description' => 'Get HTML code for IFrame widget.',
      'page callback' => 'drupal_get_form',
      'access callback' => 'user_access', 
      'access arguments' => array('view code for embedding ' . $type .' widgets'),
      'page arguments' => array($embed_widget['code form callback']),
      'type' => MENU_CALLBACK,
    );
  }
}

/**
 * Implementation of hook_embed_widget_types().
 */
function embed_widgets_iframe_embed_widget_types() {
  return array(
    'iframe' => array(
      'title' => t('Iframe'),
      'code form callback' => 'embed_widgets_iframe_code_form',
      ....
    );
  );
}

jtsnow’s picture

Status: Active » Needs review
StatusFileSize
new4.95 KB

I like the idea of having modules registering widget types. Here is a patch implementing that. It also moves the $items['embed-widgets/%embed_widgets/code/' . $type] menu items into the API module and out of the IFrame module as you suggested. This can also be done with other functions, so I will improve upon this patch.

alex ua’s picture

This is what I was suggesting (poorly) when I mentioned that you should separate the embed source (i.e. page, view, block, etc) from the embed output (i.e. iframe, javascript, etc). This is already going in that direction, but I also think it might be worth creating more hooks within the different output modules you create.

aaron’s picture

Noticed that this patch has already been committed (and expanded). The API ideas look great. In implementation, I would recommend also caching some of this, for instance embed_widgets_get_types. No need to rebuild that every time we want to build or display an image. We would just rebuild it at /admin/build/modules (and possibly other places).

Of course, you may already be planning this, and just waiting until further in development. Just putting it out there.

Great work!

Aaron

jtsnow’s picture

Status: Needs review » Closed (fixed)