You are here

function _emfield_emfield_widget in Embedded Media Field 6.3

Same name in this branch
  1. 6.3 emfield.cck.inc \_emfield_emfield_widget()
  2. 6.3 deprecated/emfield-deprecated.cck.inc \_emfield_emfield_widget()
Same name and namespace in other branches
  1. 6 emfield.cck.inc \_emfield_emfield_widget()
  2. 6.2 emfield.cck.inc \_emfield_emfield_widget()

Helper function for emfield_emfield_widget, which in turn is a help function for all emfields that implement hook_widget(). This creates default widget handling for all the Embedded Media Fields. Helper modules are expected to call this function to create the widget, which will include a list of all providers as well as handle data parsing.

1 call to _emfield_emfield_widget()
emfield_emfield_widget in deprecated/emfield-deprecated.inc

File

./emfield.cck.inc, line 167
Helper functions to implement our various cck-required functions, such as hook_field and hook_widget.

Code

function _emfield_emfield_widget(&$form, &$form_state, $field, $items, $delta = 0, $module) {

  // Our form element will need to be processed as a tree,
  // collapsing any children elements.
  $tree = array(
    '#tree' => TRUE,
  );
  $providers = emfield_allowed_providers($field, $module);
  $urls = array();
  $additional_form_elements = array();
  foreach ($providers as $provider) {

    // Only list providers allowed for this field. Honor global settings first.
    if (variable_get('emfield_' . $module . '_allow_' . $provider->name, TRUE)) {

      // Get the provider info.
      $info = emfield_include_invoke($module, $provider->name, 'info');

      // Grab the provider's URL.
      $urls[] = $info['url'] ? l($info['name'], $info['url'], array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )) : $info['name'];

      // Allow the module to add any additional elements to the form,
      // based on individual provider needs.
      $item = isset($items[$delta]) ? $items[$delta] : NULL;
      $additional_element = emfield_include_invoke($module, $provider->name, 'form', $field, $item);
      if ($additional_element) {
        $additional_form_elements[$provider->name] = $additional_element;
      }
    }
  }

  // Set the widget description, but allow the field to override this.
  if (!empty($field['widget']['description'])) {
    $textfield_description = t('!description', array(
      '!description' => content_filter_xss($field['widget']['description']),
    ));
  }
  else {
    $textfield_description = t('Enter the URL or Embed Code here. The embedded third party content will be parsed and displayed appropriately from this.');
  }

  // Add a list of all supported third party providers.
  $textfield_description .= '<br />' . t('The following services are provided: !urls', array(
    '!urls' => implode(', ', $urls),
  ));

  // Get the value of our data, if it's been set for this node.
  $embed = isset($items[$delta]['embed']) ? $items[$delta]['embed'] : '';
  $value = isset($items[$delta]['value']) ? $items[$delta]['value'] : '';
  $tree['embed'] = array(
    '#type' => 'textfield',
    '#title' => t('@label', array(
      '@label' => $field['widget']['label'],
    )),
    '#description' => $textfield_description,
    '#default_value' => $embed,
    '#required' => $delta == 0 ? $field['required'] : FALSE,
    '#maxlength' => 4096,
  );
  $tree['value'] = array(
    '#type' => 'value',
    '#value' => $embed,
  );
  if (!empty($additional_form_elements)) {
    foreach ($additional_form_elements as $key => $element) {
      $tree[$key] = $element;
    }
  }
  if ($value) {
    $info = emfield_include_invoke($module, $items[$delta]['provider'], 'info');
    $tree['value_markup'] = array(
      '#type' => 'item',
      '#value' => t('(@provider ID: !value)', array(
        '@provider' => $info['provider'],
        '!value' => l($value, emfield_include_invoke($module, $info['provider'], 'embedded_link', $value, $items[$delta]['data']), array(
          'target' => '_blank',
        )),
      )),
    );
  }

  // Allow other modules, such as Embedded Media Thumbnail,
  // to add additional elements to the widget.
  foreach (module_implements('emfield_widget_extra') as $module_extra) {

    // Use call_user_func_array() rather than module module_invoke() to
    // correctly pass the arguments as references.
    $function = $module_extra . '_emfield_widget_extra';
    $args = array(
      &$form,
      &$form_state,
      $field,
      $items,
      $delta,
      $module,
    );
    $tree[$module_extra] = call_user_func_array($function, $args);

    // In Drupal 6, we need to build multipart/form-data forms manually.
    if (function_exists($module_extra . '_emfield_widget_extra_file_included')) {
      if (call_user_func($module_extra . '_emfield_widget_extra_file_included')) {
        $form['#attributes'] = array(
          'enctype' => "multipart/form-data",
        );
      }
    }
  }
  return $tree;
}