'admin/content/emfield', 'title' => t('Embedded Media Field configuration'), 'description' => t('Configure Embedded Media Field: Allow content types to use various 3rd party providers, enter API keys, etc.'), 'callback' => 'drupal_get_form', 'callback arguments' => 'emfield_settings', 'access' => user_access('administer site configuration'), ); $items[] = array( 'path' => 'admin/content/emfield/media', 'title' => t('Media settings'), 'description' => t('Configure Embedded Media Field: Allow content types to use various 3rd party providers, enter API keys, etc.'), 'callback' => 'drupal_get_form', 'callback arguments' => 'emfield_settings', 'access' => user_access('administer site configuration'), 'type' => MENU_DEFAULT_LOCAL_TASK, ); } return $items; } /** * Callback for admin/content/emfield */ function emfield_settings() { if (!module_exists('video_cck') && !module_exists('image_ncck') && !module_exists('emaudio')) { drupal_set_message(t('The Embedded Media Field module does nothing on its own. You should also install the Embedded Video Field, Embedded Image Field, and/or Embedded Audio Field modules from the !modules. (If you do not see them listed there, under the CCK section, you may need to !download from its project page. They are all in the same package.)', array('!download' => l(t('download the module'), 'http://drupal.org/project/emfield'), '!modules' => l(t('modules administration page'), 'admin/build/modules'))), 'error'); } $form = array(); $header = array(t('Feature'), t('Supported'), t('Notes')); foreach (module_implements('emfield_info', TRUE) as $module) { $emfield_info = module_invoke($module, 'emfield_info'); $providers = emfield_system_list($module); $form[$module] = array( '#type' => 'fieldset', '#title' => t('@neighborhood', array('@neighborhood' => $emfield_info['#name'])), '#description' => $emfield_info['#settings_description'], '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form[$module]['providers'] = array( '#type' => 'fieldset', '#title' => t('Providers'), '#description' => t('The following settings determine what providers are allowed, and what provider-specific options, if any, are set.'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); foreach ($providers as $provider) { $info = emfield_include_invoke($module, $provider->name, 'info'); $form[$module]['providers'][$provider->name] = array( '#type' => 'fieldset', '#title' => t('@provider configuration', array('@provider' => $info['name'])), '#description' => $info['settings_description'], '#collapsible' => TRUE, '#collapsed' => TRUE, ); if (is_array($info['supported_features']) && !empty($info['supported_features'])) { $form[$module]['providers'][$provider->name]['supported_features'] = array( '#type' => 'fieldset', '#title' => t('Supported features'), '#description' => t('This is a list of the current state of support for the following features by %provider:', array('%provider' => $info['name'])), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 7, ); $form[$module]['providers'][$provider->name]['supported_features']['features'] = array( '#type' => 'markup', '#value' => theme('table', $header, $info['supported_features']), ); } $form[$module]['providers'][$provider->name]['emfield_' . $module . '_allow_' . $provider->name] = array( '#type' => 'checkbox', '#title' => t('Allow content from %provider', array('%provider' => $info['name'])), '#description' => t('If checked, then content types may be created that allow content to be provided by %provider.', array('%provider' => $info['name'])), '#weight' => -10, '#default_value' => variable_get('emfield_' . $module . '_allow_' . $provider->name, TRUE), ); $form[$module]['providers'][$provider->name][] = emfield_include_invoke($module, $provider->name, 'settings'); } $form[$module] = array_merge($form[$module], module_invoke($module, 'emfield_settings')); } return system_settings_form($form); } function emfield_field_columns() { $columns = array( 'embed' => array('type' => 'longtext', 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), 'provider' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), 'data' => array('type' => 'longtext', 'not null' => TRUE, 'default' => "''", 'sortable' => FALSE), ); foreach (module_implements('emfield_field_columns_extra') as $module) { $extra = module_invoke($module, 'emfield_field_columns_extra'); if (is_array($extra)) {//print_r($extra); $columns = array_merge($columns, $extra); } } return $columns; } /** Implementation of hook_emfield_field **/ function emfield_emfield_field($op, &$node, $field, &$items, $teaser, $page, $module) { switch ($op) { // TODO: nothing to validate at the moment. we need to have different provider api's have a chance to validate case 'validate': foreach ($items as $delta => $item) { $error_field = $field['multiple'] ? $field['field_name'].']['.$delta.'][embed' : $field['field_name']; _emfield_field_validate_id($field, $item, $error_field, $module); } break; case 'submit': foreach ($items as $delta => $item) { $list = _emfield_field_submit_id($field, $item, $module); $items[$delta]['provider'] = $list['provider']; $items[$delta]['value'] = $list['value']; $items[$delta]['data'] = $list['data']; } break; case 'load': // We need to unserialize the 'data' column manually $field_name = $field['field_name']; foreach ($items as $delta => $item) { $data = (array)unserialize($items[$delta]['data']); $items[$delta]['data'] = $data; $node->{$field_name}[$delta]['data'] = $data; } $return = array(); $return[$field_name] = $items; break; case 'delete': break; } // allow modules (such as emthumb) to alter our data. foreach (module_implements('emfield_field_extra') as $module) { $args = array($op, &$node, $field, &$items, $teaser, $page, $module); $ret = call_user_func_array($module . '_emfield_field_extra', $args); if (is_array($return) && is_array($ret)) { $return = array_merge($return, $ret); } } // drupal_set_message($op .' node field:
'. print_r($items, true) .''); if (in_array($op, array('insert', 'update'))) { // we need to manually serialize the 'data' array foreach ($items as $delta => $item) { $items[$delta]['data'] = serialize($items[$delta]['data']); } } return $return; } /** * return a list of providers allowed for a specific field */ function emfield_allowed_providers($field, $module) { // $module = $field['widget']['helper_module']; $allowed_providers = emfield_system_list($module); $providers = array(); $allow_all = TRUE; foreach ($allowed_providers as $test) { if (!variable_get('emfield_allow_' . $module . '_' . $test->name, TRUE)) { unset($allowed_providers[$test->name]); } else { $allow_all &= !$field['widget']['providers'][$test->name]; } } if (!$allow_all) { foreach ($allowed_providers as $test) { if (!$field['widget']['providers'][$test->name]) { unset($allowed_providers[$test->name]); } } } return $allowed_providers; } /** * this returns a list of content types that are implemented by emfield */ function emfield_implement_types($cached = TRUE) { static $types; if (!isset($types) || !$cached) { // if it's a cachable request, try to load a cached value if ($cached && $cache = cache_get('emfield_implement_types', 'cache')) { $types = unserialize($cache->data); } else { $system_types = _content_type_info(); $content_types = $system_types['content types']; $field_types = $system_types['field types']; // the array that will store type/field information for provider import $types = array(); $modules = array(); foreach (module_implements('emfield_info', TRUE) as $module) { $modules[$module] = $module; } // settings per content type for the module foreach ($content_types as $content_type => $type) { // determine which content types implement this module foreach ($type['fields'] as $field_type => $field) { // if this field type is defined by this module, then include it here if ($module = $modules[$field_types[$field['type']]['module']]) { // settings per content type per module $types[$module][$content_type][$field_type] = $field; } } } } } cache_set('emfield_implement_types', 'cache', serialize($types), CACHE_PERMANENT); return $types; } /** * This will parse the url or embedded code pasted by the node submitter. * returns either an empty array (if no match), or an array of provider and value. */ function emfield_parse_embed($field, $embed = '', $module) { // if ($embed) { // $module = $field['widget']['helper_module']; $providers = emfield_allowed_providers($field, $module); foreach ($providers as $provider) { $success = emfield_include_invoke($module, $provider->name, 'extract', $embed, $field); // we've been given an array of regex strings, so let's see if we can find a match if (is_array($success)) { foreach ($success as $regex) { $matches = NULL; if (preg_match($regex, $embed, $matches)) { return array('provider' => $provider->name, 'value' => $matches[1]); } } } // the invoked include module did its own parsing and found a match else if ($success) { return array('provider' => $provider->name, 'value' => $success); } } // } // we found no match return array(); } /** * extract the id from embedded code or url */ function _emfield_field_validate_id($field, $item, $error_field, $module) { // does nothing at this time... TODO: fix this up return _emfield_field_submit_id($field, $item, $module, $error_field); } /** * replace embedded code with the extracted id. this goes in the field 'value' * also allows you to grab directly from the URL to display the content from field 'provider' */ function _emfield_field_submit_id($field, $item, $module) { // $module = $field['widget']['helper_module']; $item = array_merge($item, emfield_parse_embed($field, $item['embed'], $module)); $item['data'] = (array)emfield_include_invoke($module, $item['provider'], 'data', $field, $item); return $item; } function emfield_emfield_field_formatter($field, $item, $formatter, $node, $module) { // if we're coming from a preview, we need to extract our new embedded value... if ($item['in_preview']) { $item = emfield_parse_embed($field, $item['embed'], $module); } // if we have no value, then return an empty string if (!isset($item['value'])) { return ''; } // unfortunately, when we come from a view, we don't get all the widget fields if (!$node->type) { $type = content_types($field['type_name']); $field['widget'] = $type['fields'][$field['field_name']]['widget']; } // and sometimes our data is still unserialized, again from views if (!is_array($item['data'])) { $item['data'] = (array)unserialize($item['data']); } // $module = $field['widget']['helper_module']; $output .= theme($module . '_' . $formatter, $field, $item, $formatter, $node); return $output; } /** Widgets **/ function emfield_emfield_widget_settings($op, $widget, $module) { switch ($op) { case 'form': // make sure to register the new type as supported by this module emfield_implement_types(FALSE); $form = array(); $options = array(); $providers = emfield_system_list($module); foreach ($providers as $provider) { if (variable_get('emfield_allow_' . $module . '_' . $provider->name, TRUE)) { $info = emfield_include_invoke($module, $provider->name, 'info'); $options[$provider->name] = $info['name']; } } $form['provider_list'] = array( '#type' => 'fieldset', '#title' => t('Providers Supported'), '#description' => t('Select which third party providers you wish to allow for this content type from the list below. If no checkboxes are checked, then all providers will be supported. When a user submits new content, the URL they enter will be matched to the provider, assuming that provider is allowed here.'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['provider_list']['providers'] = array( '#type' => 'checkboxes', '#title' => t('Providers'), '#default_value' => $widget['providers'] ? $widget['providers'] : array(), '#options' => $options, ); foreach (module_implements('emfield_widget_settings_extra') as $module) { $form[$module] = module_invoke($module, 'emfield_widget_settings_extra', 'form', $widget); } return $form; case 'save': $columns = array('providers'); //, 'helper_module', ); foreach (module_implements('emfield_widget_settings_extra') as $module) { $columns = array_merge($columns, module_invoke($module, 'emfield_widget_settings_extra', 'save', $widget)); } return $columns; } } 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[] = l($info['name'], $info['url'], array('target' => '_blank')); $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 = $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 .= '