You are here

function emfield_widget in Embedded Media Field 6.3

Implementation of hook_widget()

File

./emfield.module, line 204
Embedded Media Field is a CCK-based framework for embedding media files.

Code

function emfield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  static $js_added;

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

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

  // 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.
  $uri = isset($items[$delta]['uri']) ? $items[$delta]['uri'] : '';
  $tree['uri'] = array(
    '#type' => 'textfield',
    '#title' => t('@label', array(
      '@label' => $field['widget']['label'],
    )),
    //     '#description' => $textfield_description,
    '#default_value' => $uri,
    '#required' => $delta == 0 ? $field['required'] : FALSE,
    '#maxlength' => 255,
    '#attributes' => array(
      'class' => 'emfield-textfield-uri',
    ),
  );
  $tree['url'] = array(
    '#type' => 'textfield',
    '#title' => t('URL: @TODO launch browser instead.'),
    '#description' => $textfield_description,
    '#maxlength' => 4096,
    '#attributes' => array(
      'class' => 'emfield-textfield-url form-autocomplete',
    ),
  );
  if (!$js_added) {
    $js_added = TRUE;
    drupal_add_js(drupal_get_path('module', 'emfield') . '/includes/js/emfield.widget.js');
    drupal_add_js(array(
      'emfield' => array(
        'parseUrl' => url('emapi/parse/json'),
      ),
    ), 'setting');
    drupal_add_css(drupal_get_path('module', 'emfield') . '/includes/themes/css/emfield.widget.css');
  }
  if ($uri) {
    $media = emapi_media_from_uri($uri);
    $class = emapi_get_provider_classes(emapi_uri_scheme($uri));
    if (is_array($class) && $class['name']) {
      $name = $class['name'];
      $url = $media
        ->url();
      $link = l($uri, $url, array(
        'attributes' => array(
          'target' => '_blank',
        ),
      ));
      $tree['value_markup'] = array(
        '#type' => 'item',
        '#value' => t('(@provider ID: !value)', array(
          '@provider' => $name,
          '!value' => $link,
        )),
      );
      $tree['url']['#default_value'] = check_plain($media
        ->url());
    }
  }
  return $tree;
}