You are here

function emfield_emfield_widget in Embedded Media Field 5

Same name and namespace in other branches
  1. 6.3 deprecated/emfield-deprecated.inc \emfield_emfield_widget()
  2. 6 emfield.module \emfield_emfield_widget()
  3. 6.2 emfield.module \emfield_emfield_widget()
3 calls to emfield_emfield_widget()
emaudio_widget in contrib/emaudio/emaudio.module
Implementation of hook_widget *
image_ncck_widget in contrib/image_ncck/image_ncck.module
video_cck_widget in contrib/video_cck/video_cck.module
Implementation of hook_widget *

File

./emfield.module, line 390

Code

function emfield_emfield_widget($op, &$node, $field, &$node_field, $module) {

  //  $module = $field['widget']['helper_module'];
  switch ($op) {
    case 'prepare form values':

      // Don't save empty fields except the first value
      foreach ($node_field as $delta => $item) {
        if ($item['value'] == '' && $delta > 0) {
          unset($node_field[$delta]);
        }
      }

      //print 'nodefield:' ; print_r($node_field);
      foreach (module_implements('emfield_widget_extra') as $module) {
        $args = array(
          $op,
          &$node,
          $field,
          &$node_field,
          $module,
        );
        call_user_func_array($module . '_emfield_widget_extra', $args);
      }
      break;
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      $textfield = 'embed';
      $providers = emfield_allowed_providers($field, $module);
      $urls = array();
      $additional_form_elements = array();
      foreach ($providers as $provider) {

        // don't check providers not allowed
        if (variable_get('emfield_allow_' . $module . '_' . $provider->name, TRUE)) {
          $info = emfield_include_invoke($module, $provider->name, 'info');
          $urls[] = $info['url'] ? l($info['name'], $info['url'], array(
            'target' => '_blank',
          )) : $info['name'];
          $additional_element = emfield_include_invoke($module, $provider->name, 'form');
          if ($additional_element) {
            $additional_form_elements[$provider->name] = $additional_element;
          }
        }
      }
      $textfield_title = t($field['widget']['label']);
      if (!empty($field['widget']['description'])) {
        $textfield_description = t('@description', array(
          '@description' => $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.');
      }
      $textfield_description .= '<br />' . t('The following services are provided: !urls', array(
        '!urls' => implode(', ', $urls),
      ));
      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#title'] = t('@label', array(
          '@label' => $field['widget']['label'],
        ));
        $delta = 0;
        foreach ($node_field as $data) {
          if (isset($data[$textfield])) {
            $form[$field['field_name']][$delta][$textfield] = array(
              '#type' => 'textfield',
              '#title' => $textfield_title,
              '#description' => $textfield_description,
              '#default_value' => $data[$textfield],
              '#required' => $delta == 0 ? $field['required'] : FALSE,
              '#maxlength' => 4096,
            );
            if (!empty($additional_form_elements)) {
              foreach ($additional_form_elements as $key => $element) {
                $form[$field['field_name']][$delta][$key] = $element;
              }
            }
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'value',
              '#value' => $data['value'],
            );
            if ($data['value']) {
              $info = emfield_include_invoke($module, $data['provider'], 'info');
              $form[$field['field_name']][$delta]['value_markup'] = array(
                '#type' => 'item',
                '#value' => t('(@provider ID: !value)', array(
                  '@provider' => $info['name'],
                  '!value' => l($data['value'], emfield_include_invoke($module, $info['provider'], 'embedded_link', $data['value'], $data['data']), array(
                    'target' => '_blank',
                  )),
                )),
              );
            }
            foreach (module_implements('emfield_widget_extra') as $module) {
              $form[$field['field_name']][$delta][$module] = module_invoke($module, 'emfield_widget_extra', 'form', $node, $field, $data);
            }
            $delta++;
          }
        }
        foreach (range($delta, $delta + 2) as $delta) {
          $form[$field['field_name']][$delta][$textfield] = array(
            '#type' => 'textfield',
            '#title' => $textfield_title,
            '#description' => $textfield_description,
            '#default_value' => '',
            '#required' => $delta == 0 ? $field['required'] : FALSE,
            '#maxlength' => 2048,
          );
          if (!empty($additional_form_elements)) {
            foreach ($additional_form_elements as $key => $element) {
              $form[$field['field_name']][$delta][$key] = $element;
            }
          }
          $form[$field['field_name']][$delta]['value'] = array(
            '#type' => 'value',
            '#title' => '',
          );
          foreach (module_implements('emfield_widget_extra') as $module) {
            $form[$field['field_name']][$delta][$module] = module_invoke($module, 'emfield_widget_extra', 'form', $node, $field, $data);
          }
        }
      }
      else {
        $form[$field['field_name']][0][$textfield] = array(
          '#type' => 'textfield',
          '#title' => $textfield_title,
          //t($field['widget']['label']),
          '#description' => $textfield_description,
          '#default_value' => isset($node_field[0][$textfield]) ? $node_field[0][$textfield] : '',
          '#required' => $field['required'],
          '#maxlength' => 2048,
        );
        if ($textfield == 'embed') {
          $value = isset($node_field[0]['value']) ? $node_field[0]['value'] : '';
          $form[$field['field_name']][0]['value'] = array(
            '#type' => 'value',
            '#value' => $value,
          );
          if (!empty($additional_form_elements)) {
            foreach ($additional_form_elements as $key => $element) {
              $form[$field['field_name']][0][$key] = $element;
            }
          }
          if ($value) {
            $info = emfield_include_invoke($module, $node_field[0]['provider'], 'info');
            $form[$field['field_name']][0]['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, $node_field[0]['data']), array(
                  'target' => '_blank',
                )),
              )),
            );
          }
          foreach (module_implements('emfield_widget_extra') as $module) {
            $form[$field['field_name']][$delta][$module] = module_invoke($module, 'emfield_widget_extra', 'form', $node, $field, $node_field[0]);
          }
        }
      }
      return $form;
    default:
      break;
  }
}