diff --git a/sites/all/modules/contrib/fields/field_collection/LICENSE.txt b/sites/all/modules/contrib/fields/field_collection/LICENSE.txt new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/LICENSE.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/sites/all/modules/contrib/fields/field_collection/README.txt b/sites/all/modules/contrib/fields/field_collection/README.txt new file mode 100644 index 0000000..e027040 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/README.txt @@ -0,0 +1,36 @@ + +Field collection +----------------- +Provides a field collection field, to which any number of fields can be attached. + +Each field collection item is internally represented as an entity, which is +referenced via the field collection field in the host entity. While +conceptually field collections are treated as part of the host entity, each +field collection item may also be viewed and edited separately. + + + Usage + ------ + + * Add a field collection field to any entity, e.g. to a node. For that use the + the usual "Manage fields" interface provided by the "field ui" module of + Drupal, e.g. "Admin -> Structure-> Content types -> Article -> Manage fields". + + * Then go to "Admin -> Structure-> Field collection" to define some fields for + the created field collection. + + * By the default, the field collection is not shown during editing of the host + entity. However, some links for adding, editing or deleting field collection + items is shown when the host entity is viewed. + + * Widgets for embedding the form for creating field collections in the + host-entity can be provided by any module. In future the field collection + module might provide such widgets itself too. + + +Restrictions +------------- + + * As of now, the field collection field does not properly respect field + translation. Thus, for now it is suggested to only use the field for + entities that are not translatable. diff --git a/sites/all/modules/contrib/fields/field_collection/ctools/relationships/field_collection_from_field.inc b/sites/all/modules/contrib/fields/field_collection/ctools/relationships/field_collection_from_field.inc new file mode 100644 index 0000000..58d970c --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/ctools/relationships/field_collection_from_field.inc @@ -0,0 +1,106 @@ + t('Field collection item'), + 'description' => t('Creates an entity context from a field collection field on a field.'), + 'context' => 'field_collection_field_collection_from_field_context', + 'edit form' => 'field_collection_field_collection_from_field_edit_form', + 'get child' => 'field_collection_field_collection_from_field_get_child', + 'get children' => 'field_collection_field_collection_from_field_get_children', + 'defaults' => array('delta' => 0), +); + +/** + * Get child callback. + */ +function field_collection_field_collection_from_field_get_child($plugin, $parent, $child) { + $plugins = field_collection_field_collection_from_field_get_children($plugin, $parent); + return $plugins[$parent . ':' . $child]; +} + +/** + * Get children callback. + */ +function field_collection_field_collection_from_field_get_children($plugin, $parent) { + + $plugins = array(); + $instances_info = field_info_instances(); + if (isset($instances_info['field_collection_item'])) { + $field_collection_items = $instances_info['field_collection_item']; + + foreach (field_read_instances() as $instance) { + if (isset($field_collection_items[$instance['field_name']])) { + $child_plugin_id = $parent . ':' . $instance['entity_type'] . ':' . $instance['bundle'] . ':' . $instance['field_name']; + + $child_plugin = $plugin; + $child_plugin['context name'] = $instance['entity_type'] . ':' . $instance['bundle'] . ':' . $instance['field_name']; + $child_plugin['title'] = t( + '!label field collection (!field_name) from !entity_type (!bundle)', + array( + '!label' => $instance['label'], + '!field_name' => $instance['field_name'], + '!entity_type' => $instance['entity_type'], + '!bundle' => $instance['bundle'] + ) + ); + $restrictions = array('type' => array($instance['bundle'])); + $child_plugin['required context'] = new ctools_context_required(ucfirst($instance['entity_type']), $instance['entity_type'], $restrictions); + $child_plugin['parent'] = $parent; + $child_plugin['keyword'] = 'Field collection'; + $child_plugin['entity_type'] = $instance['entity_type']; + $child_plugin['field_name'] = $instance['field_name']; + + $child_plugin['name'] = $child_plugin_id; + $plugins[$child_plugin_id] = $child_plugin; + + } + } + } + + return $plugins; +} + +/** + * Return a new field collection context based on an existing context. + */ +function field_collection_field_collection_from_field_context($context, $conf) { + + $plugin_info = ctools_get_relationship($conf['name']); + $delta = (int) $conf['delta']; + + $entity = $context->data; + if (isset($entity->{$plugin_info['field_name']})) { + + $items = field_get_items($plugin_info['entity_type'], $entity, $plugin_info['field_name']); + if (isset($items[$delta]['value'])) { + $field_collection_item = field_collection_item_load($items[$delta]['value']); + } + + return ctools_context_create('entity:field_collection_item', $items[$delta]['value']); + } + + return ctools_context_create_empty('entity:field_collection_item', NULL); +} + +/** + * Settings form. + */ +function field_collection_field_collection_from_field_edit_form($form, &$form_state) { + $conf = $form_state['conf']; + + $form['delta'] = array( + '#type' => 'textfield', + '#title' => t('Delta'), + '#size' => 3, + '#description' => t('The relationship can only create one context, but multiple items can be related. Please type in the number you want. The first one will be 0.'), + '#default_value' => empty($conf['delta']) ? 0 : $conf['delta'], + ); + + return $form; +} diff --git a/sites/all/modules/contrib/fields/field_collection/field-collection-item.tpl.php b/sites/all/modules/contrib/fields/field_collection/field-collection-item.tpl.php new file mode 100644 index 0000000..8d345cb --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field-collection-item.tpl.php @@ -0,0 +1,37 @@ + +
> +
> + +
+
diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.admin.inc b/sites/all/modules/contrib/fields/field_collection/field_collection.admin.inc new file mode 100644 index 0000000..b6328a4 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.admin.inc @@ -0,0 +1,47 @@ + t('Operations'), 'colspan' => '2')); + $rows = array(); + foreach ($instances as $entity_type => $type_bundles) { + foreach ($type_bundles as $bundle => $bundle_instances) { + foreach ($bundle_instances as $field_name => $instance) { + $field = field_info_field($field_name); + if ($field['type'] == 'field_collection') { + $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle); + $rows[$field_name]['class'] = $field['locked'] ? array('menu-disabled') : array(''); + + $rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name; + $rows[$field_name]['data'][1][] = l($bundles[$entity_type][$bundle]['label'], $admin_path . '/fields'); + } + } + } + } + foreach ($rows as $field_name => $cell) { + $rows[$field_name]['data'][1] = implode(', ', $cell['data'][1]); + + $field_name_url_str = strtr($field_name, array('_' => '-')); + $rows[$field_name]['data'][2] = l(t('manage fields'), 'admin/structure/field-collections/' . $field_name_url_str . '/fields'); + $rows[$field_name]['data'][3] = l(t('manage display'), 'admin/structure/field-collections/' . $field_name_url_str . '/display'); + } + if (empty($rows)) { + $output = t('No field collections have been defined yet. To do so attach a field collection field to any entity.'); + } + else { + // Sort rows by field name. + ksort($rows); + $output = theme('table', array('header' => $header, 'rows' => $rows)); + } + return $output; +} diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.api.php b/sites/all/modules/contrib/fields/field_collection/field_collection.api.php new file mode 100644 index 0000000..28e8273 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.api.php @@ -0,0 +1,169 @@ +my_field) && empty($item->my_field)) { + $is_empty = TRUE; + } +} + +/** + * Acts on field collections being loaded from the database. + * + * This hook is invoked during field collection item loading, which is handled + * by entity_load(), via the EntityCRUDController. + * + * @param array $entities + * An array of field collection item entities being loaded, keyed by id. + * + * @see hook_entity_load() + */ +function hook_field_collection_item_load(array $entities) { + $result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($entities))); + foreach ($result as $record) { + $entities[$record->pid]->foo = $record->foo; + } +} + +/** + * Responds when a field collection item is inserted. + * + * This hook is invoked after the field collection item is inserted into the + * database. + * + * @param FieldCollectionItemEntity $field_collection_item + * The field collection item that is being inserted. + * + * @see hook_entity_insert() + */ +function hook_field_collection_item_insert(FieldCollectionItemEntity $field_collection_item) { + db_insert('mytable')->fields(array( + 'id' => entity_id('field_collection_item', $field_collection_item), + 'extra' => print_r($field_collection_item, TRUE), + ))->execute(); +} + +/** + * Acts on a field collection item being inserted or updated. + * + * This hook is invoked before the field collection item is saved to the database. + * + * @param FieldCollectionItemEntity $field_collection_item + * The field collection item that is being inserted or updated. + * + * @see hook_entity_presave() + */ +function hook_field_collection_item_presave(FieldCollectionItemEntity $field_collection_item) { + $field_collection_item->name = 'foo'; +} + +/** + * Responds to a field collection item being updated. + * + * This hook is invoked after the field collection item has been updated in the + * database. + * + * @param FieldCollectionItemEntity $field_collection_item + * The field collection item that is being updated. + * + * @see hook_entity_update() + */ +function hook_field_collection_item_update(FieldCollectionItemEntity $field_collection_item) { + db_update('mytable') + ->fields(array('extra' => print_r($field_collection_item, TRUE))) + ->condition('id', entity_id('field_collection_item', $field_collection_item)) + ->execute(); +} + +/** + * Responds to field collection item deletion. + * + * This hook is invoked after the field collection item has been removed from + * the database. + * + * @param FieldCollectionItemEntity $field_collection_item + * The field collection item that is being deleted. + * + * @see hook_entity_delete() + */ +function hook_field_collection_item_delete(FieldCollectionItemEntity $field_collection_item) { + db_delete('mytable') + ->condition('pid', entity_id('field_collection_item', $field_collection_item)) + ->execute(); +} + +/** + * Act on a field collection item that is being assembled before rendering. + * + * @param $field_collection_item + * The field collection item entity. + * @param $view_mode + * The view mode the field collection item is rendered in. + * @param $langcode + * The language code used for rendering. + * + * The module may add elements to $field_collection_item->content prior to + * rendering. The structure of $field_collection_item->content is a renderable + * array as expected by drupal_render(). + * + * @see hook_entity_prepare_view() + * @see hook_entity_view() + */ +function hook_field_collection_item_view($field_collection_item, $view_mode, $langcode) { + $field_collection_item->content['my_additional_field'] = array( + '#markup' => $additional_field, + '#weight' => 10, + '#theme' => 'mymodule_my_additional_field', + ); +} + +/** + * Alter the results of entity_view() for field collection items. + * + * This hook is called after the content has been assembled in a structured + * array and may be used for doing processing which requires that the complete + * field collection item content structure has been built. + * + * If the module wishes to act on the rendered HTML of the field collection item + * rather than the structured content array, it may use this hook to add a + * #post_render callback. See drupal_render() and theme() documentation + * respectively for details. + * + * @param $build + * A renderable array representing the field collection item content. + * + * @see hook_entity_view_alter() + */ +function hook_field_collection_item_view_alter($build) { + if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) { + // Change its weight. + $build['an_additional_field']['#weight'] = -10; + + // Add a #post_render callback to act on the rendered HTML of the entity. + $build['#post_render'][] = 'my_module_post_render'; + } +} + +/** + * @} + */ \ No newline at end of file diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.info b/sites/all/modules/contrib/fields/field_collection/field_collection.info new file mode 100644 index 0000000..368128d --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.info @@ -0,0 +1,17 @@ +name = Field collection +description = Provides a field collection field, to which any number of fields can be attached. +core = 7.x +dependencies[] = entity +files[] = field_collection.test +files[] = field_collection.info.inc +files[] = views/field_collection_handler_relationship.inc +files[] = field_collection.migrate.inc +configure = admin/structure/field-collections +package = Fields + +; Information added by Drupal.org packaging script on 2014-11-04 +version = "7.x-1.0-beta8" +core = "7.x" +project = "field_collection" +datestamp = "1415122384" + diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.info.inc b/sites/all/modules/contrib/fields/field_collection/field_collection.info.inc new file mode 100644 index 0000000..03c961a --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.info.inc @@ -0,0 +1,30 @@ + t('Host entity'), + 'type' => 'entity', + 'description' => t('The entity containing the field collection field.'), + 'getter callback' => 'field_collection_item_get_host_entity', + 'setter callback' => 'field_collection_item_set_host_entity', + 'required' => TRUE, + ); + + return $info; + } + +} \ No newline at end of file diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.install b/sites/all/modules/contrib/fields/field_collection/field_collection.install new file mode 100644 index 0000000..d2b1892 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.install @@ -0,0 +1,330 @@ + 'Stores information about field collection items.', + 'fields' => array( + 'item_id' => array( + 'type' => 'serial', + 'not null' => TRUE, + 'description' => 'Primary Key: Unique field collection item ID.', + ), + 'revision_id' => array( + 'type' => 'int', + 'not null' => TRUE, + 'description' => 'Default revision ID.', + ), + 'field_name' => array( + 'description' => 'The name of the field on the host entity embedding this entity.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + ), + 'archived' => array( + 'description' => 'Boolean indicating whether the field collection item is archived.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + ), + 'primary key' => array('item_id'), + ); + $schema['field_collection_item_revision'] = array( + 'description' => 'Stores revision information about field collection items.', + 'fields' => array( + 'revision_id' => array( + 'type' => 'serial', + 'not null' => TRUE, + 'description' => 'Primary Key: Unique revision ID.', + ), + 'item_id' => array( + 'type' => 'int', + 'not null' => TRUE, + 'description' => 'Field collection item ID.', + ), + ), + 'primary key' => array('revision_id'), + 'indexes' => array( + 'item_id' => array('item_id'), + ), + 'foreign keys' => array( + 'versioned_field_collection_item' => array( + 'table' => 'field_collection_item', + 'columns' => array('item_id' => 'item_id'), + ), + ), + ); + return $schema; +} + +/** + * Implements hook_field_schema(). + */ +function field_collection_field_schema($field) { + $columns = array( + 'value' => array( + 'type' => 'int', + 'not null' => FALSE, + 'description' => 'The field collection item id.', + ), + 'revision_id' => array( + 'type' => 'int', + 'not null' => FALSE, + 'description' => 'The field collection item revision id.', + ), + ); + return array( + 'columns' => $columns, + 'indexes' => array( + 'revision_id' => array('revision_id'), + ), + ); +} + +/** + * Update the administer field collection permission machine name. + */ +function field_collection_update_7000() { + db_update('role_permission') + ->fields(array('permission' => 'administer field collections')) + ->condition('permission', 'administer field-collections') + ->execute(); +} + +/** + * Add revision support. + */ +function field_collection_update_7001() { + + // Add revision_id column to field_collection_item table. + $revision_id_spec = array( + 'type' => 'int', + 'not null' => TRUE, + 'description' => 'Default revision ID.', + // Set default to 0 temporarily. + 'initial' => 0, + ); + // Field may already exist due to bug in 7.x-1.0-beta5. + if (!db_field_exists('field_collection_item', 'revision_id')) { + db_add_field('field_collection_item', 'revision_id', $revision_id_spec); + } + + // Initialize the revision_id to be the same as the item_id. + db_update('field_collection_item') + ->expression('revision_id', 'item_id') + ->execute(); + + // Add the archived column + $archived_spec = array( + 'description' => 'Boolean indicating whether the field collection item is archived.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ); + // Field may already exist due to bug in 7.x-1.0-beta5. + if (!db_field_exists('field_collection_item', 'archived')) { + db_add_field('field_collection_item', 'archived', $archived_spec); + } + + // Create the new table. It is important to explicitly define the schema here + // rather than use the hook_schema definition: http://drupal.org/node/150220. + $schema['field_collection_item_revision'] = array( + 'description' => 'Stores revision information about field collection items.', + 'fields' => array( + 'revision_id' => array( + 'type' => 'serial', + 'not null' => TRUE, + 'description' => 'Primary Key: Unique revision ID.', + ), + 'item_id' => array( + 'type' => 'int', + 'not null' => TRUE, + 'description' => 'Field collection item ID.', + ), + ), + 'primary key' => array('revision_id'), + 'indexes' => array( + 'item_id' => array('item_id'), + ), + 'foreign keys' => array( + 'versioned_field_collection_item' => array( + 'table' => 'field_collection_item', + 'columns' => array('item_id' => 'item_id'), + ), + ), + ); + // Table may already exist due to bug in 7.x-1.0-beta5. + if (db_table_exists('field_collection_item_revision')) { + db_drop_table('field_collection_item_revision'); + } + db_create_table('field_collection_item_revision', $schema['field_collection_item_revision']); + + // Fill the new table with the correct data. + $items = db_select('field_collection_item', 'fci') + ->fields('fci') + ->execute(); + foreach ($items as $item) { + // Update field_collection_item_revision table. + db_insert('field_collection_item_revision') + ->fields(array( + 'revision_id' => $item->item_id, + 'item_id' => $item->item_id, + )) + ->execute(); + } + + // Update the field_collection_field_schema columns for all tables. + // Add a revision_id column. + $revision_id_spec['description'] = 'The field collection item revision id.'; + // Because $value_column below can be null, so must $revision_id_column. + $revision_id_spec['not null'] = FALSE; + foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) { + $table_prefixes = array('field_data', 'field_revision'); + foreach ($table_prefixes as $table_prefix) { + + $table = sprintf('%s_%s', $table_prefix, $field_name); + $value_column = sprintf('%s_value', $field_name); + $revision_id_column = sprintf('%s_revision_id', $field_name); + + // Field may already exist due to bug in 7.x-1.0-beta5. + if (!db_field_exists($table, $revision_id_column)) { + db_add_field($table, $revision_id_column, $revision_id_spec); + } + else { + db_change_field($table, $revision_id_column, $revision_id_column, $revision_id_spec); + } + + // Initialize the revision_id to be the same as the item_id. + db_update($table) + ->expression($revision_id_column, $value_column) + ->execute(); + } + } + + // Need to get the system up-to-date so drupal_schema_fields_sql() will work. + $schema = drupal_get_schema('field_collection_item_revision', TRUE); +} + +/** + * Remove orphaned field collection item entities. + */ +function field_collection_update_7002() { + // Loop over all fields and delete any orphaned field collection items. + foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) { + + $select = db_select('field_collection_item', 'fci') + ->fields('fci', array('item_id')) + ->condition('field_name', $field_name) + ->condition('archived', 0); + $select->leftJoin('field_data_' . $field_name, 'field', "field.{$field_name}_value = fci.item_id "); + $select->isNull('field.entity_id'); + $ids = $select->execute()->fetchCol(0); + + entity_delete_multiple('field_collection_item', $ids); + drupal_set_message(t('Deleted @count orphaned field collection items.', array('@count' => count($ids)))); + } +} + +/** + * Update field_collection_field_schema columns for all tables. + */ +function field_collection_update_7003() { + // Revision_id column. + $revision_id_spec = array( + 'type' => 'int', + 'not null' => FALSE, + 'description' => 'The field collection item revision id.', + 'initial' => 0, + ); + + // Update the field_collection_field_schema columns for all tables, + // in case the buggy beta5 version of field_collection_update_7001() + // completed without complaint. + foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) { + $table_prefixes = array('field_data', 'field_revision'); + foreach ($table_prefixes as $table_prefix) { + $table = sprintf('%s_%s', $table_prefix, $field_name); + $value_column = sprintf('%s_value', $field_name); + $revision_id_column = sprintf('%s_revision_id', $field_name); + db_change_field($table, $revision_id_column, $revision_id_column, $revision_id_spec); + } + } + + // Need to get the system up-to-date so drupal_schema_fields_sql() will work. + $schema = drupal_get_schema('field_collection_item_revision', TRUE); +} + +/** + * Add index on {$field_collection_field}_revision_id column for all tables. + */ +function field_collection_update_7004() { + // Update the field_collection_field_schema columns for all tables. + foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) { + $table_prefixes = array('field_data', 'field_revision'); + foreach ($table_prefixes as $table_prefix) { + + $table = sprintf('%s_%s', $table_prefix, $field_name); + $revision_id_column = sprintf('%s_revision_id', $field_name); + + // Add index on revision_id column. + if (!db_index_exists($table, $revision_id_column)) { + db_add_index($table, $revision_id_column, array($revision_id_column)); + } + } + } +} + +/** + * Force the creation of the table cache_entity_field_collection_item. + * + * entity_update_7003 will attempt to install entitycache tables for existing + * modules, but it uses module_list() to get the list of available modules, + * which, when called from a database update, may not return field_collection + * since drupal is bootstrapped at a lower level. + */ +function field_collection_update_7005() { + if (module_exists('entitycache')) { + $entity_type = 'field_collection_item'; + $table = 'cache_entity_' . $entity_type; + if (!db_table_exists($table)) { + $schema = drupal_get_schema_unprocessed('system', 'cache'); + $schema['description'] = 'Cache table used to store' . $entity_type . ' entity records.'; + db_create_table($table, $schema); + } + } +} + +/** + * Ensures revision_id indexes are present at field_config table. + */ +function field_collection_update_7006() { + $result = db_query("SELECT id, field_name, data FROM {field_config} WHERE type = 'field_collection'"); + foreach ($result as $field_config) { + $data = unserialize($field_config->data); + // Skip this record if the revision_id index is already present. + if (isset($data['indexes']['revision_id'])) { + continue; + } + // Otherwise, add the revision_id index and update the record. + $data['indexes']['revision_id'] = array('revision_id'); + $data = serialize($data); + $num_updated = db_update('field_config') + ->fields(array('data' => $data)) + ->condition('id', $field_config->id) + ->execute(); + // If for some reason the update failed, throw an exception. + if ($num_updated != 1) { + $t_args['@field'] = $field_config->field_name; + throw new DrupalUpdateException(t('An error was detected when attempting to update field configuration for field @field.', $t_args)); + } + } +} diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.migrate.inc b/sites/all/modules/contrib/fields/field_collection/field_collection.migrate.inc new file mode 100644 index 0000000..a5ac706 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.migrate.inc @@ -0,0 +1,181 @@ +dependencies = array('Article'); + * + * $this->destination = new MigrateDestinationFieldCollection( + * 'field_attached_data', + * array('host_entity_type' => 'node') + * ); + * + * $this->addFieldMapping('host_entity_id', 'source_article_id') + * ->sourceMigration('Article'); + * @endcode + * + * @see http://drupal.org/node/1900640 + */ + +/** + * Destination class implementing migration into field_collection. + */ +class MigrateDestinationFieldCollection extends MigrateDestinationEntity { + /** + * The type of entity hosting this collection field (e.g., node). + * + * @var string + */ + protected $hostEntityType; + + static public function getKeySchema() { + return array( + 'item_id' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => 'ID of field collection item', + ), + ); + } + + /** + * Basic initialization. + * + * @param string $bundle + * Bundle name. + * @param array $options + * (optional) Options applied to collections. + */ + public function __construct($bundle, array $options = array()) { + parent::__construct('field_collection_item', $bundle, $options); + $this->hostEntityType = $options['host_entity_type']; + } + + /** + * Returns a list of fields available to be mapped for this collection + * (bundle). + * + * @return array + * Keys: machine names of the fields (to be passed to addFieldMapping). + * Values: Human-friendly descriptions of the fields. + */ + public function fields() { + $fields = migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle); + $fields['item_id'] = t('Field collection entity ID'); + $fields['host_entity_id'] = t('Field collection host ID'); + return $fields; + } + + /** + * Import a single field collection item. + * + * @param $collection + * Collection object to build. Pre-filled with any fields mapped in the + * migration. + * @param $row + * Raw source data object - passed through to prepare/complete handlers. + * + * @return array|false + * Array of key fields (item_id only in this case) of the collection that + * was saved or FALSE on failure. + */ + public function import(stdClass $collection, stdClass $row) { + if (isset($row->migrate_map_destid1)) { + // We're updated an existing entity - start from the previous data. + // entity_load() returns an array, so we get the field collection entity + // with array_shift(). + $entity = array_shift(entity_load('field_collection_item', array($row->migrate_map_destid1), array(), TRUE)); + $entity_old = clone $entity; + $updating = TRUE; + } + else { + // Skip the collection if it has no host. + if (empty($collection->host_entity_id)) { + throw new MigrateException('Could not find host entity of the field collection to import.'); + } + $entity = entity_create('field_collection_item', array('field_name' => $this->bundle)); + $updating = FALSE; + $host_entity = entity_load_single($this->hostEntityType, $collection->host_entity_id); + entity_get_controller($this->hostEntityType)->resetCache(); + $entity->setHostEntity($this->hostEntityType, $host_entity); + } + + unset($collection->host_entity_id); + + foreach ((array) $collection as $field => $value) { + $entity->{$field} = $value; + } + + $this->prepare($entity, $row); + + // Restore fields from original field_collection_item if updating + if ($updating) { + foreach ($entity as $field => $value) { + if ('field_' != substr($field, 0, 6)) { + continue; + } + elseif (property_exists($entity_old, $field) && !property_exists($collection, $field)) { + $entity->$field = $entity_old->$field; + } + } + } + + migrate_instrument_start('field_collection_save'); + $status = entity_save('field_collection_item', $entity); + migrate_instrument_stop('field_collection_save'); + + if (in_array($this->hostEntityType, array('node', 'field_collection_item')) || ($status !== FALSE)) { + $this->complete($entity, $row); + if ($updating) { + $this->numUpdated++; + } + else { + $this->numCreated++; + } + return array($entity->item_id); + } + else { + return FALSE; + } + } + + /** + * Delete a migrated collection. + * + * @param $key + * Array of fields representing the key. + */ + public function rollback(array $key) { + $item_id = reset($key); + + $this->prepareRollback($item_id); + $field_collection_item = field_collection_item_load($item_id); + // If the collection wasn't imported then we can't roll it back, so check if + // the loaded object is an instance of the FieldCollectionItemEntity class. + if ($field_collection_item instanceof FieldCollectionItemEntity) { + $field_collection_item->delete(); + } + + $this->completeRollback($item_id); + return TRUE; + } +} + +/** + * Implements migrate hook_migrate_api(). + */ +function field_collection_migrate_api() { + $api = array( + 'api' => 2, + ); + return $api; +} diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.module b/sites/all/modules/contrib/fields/field_collection/field_collection.module new file mode 100644 index 0000000..3a8f9e9 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.module @@ -0,0 +1,1998 @@ +' . t('About') . ''; + $output .= '

' . t('The field collection module provides a field, to which any number of fields can be attached. See the Field module help page for more information about fields.', array('@field-help' => url('admin/help/field'))) . '

'; + return $output; + } +} + +/** + * Implements hook_ctools_plugin_directory(). + */ +function field_collection_ctools_plugin_directory($module, $plugin) { + if ($module == 'ctools') { + return 'ctools/' . $plugin; + } +} + +/** + * Implements hook_entity_info(). + */ +function field_collection_entity_info() { + $return['field_collection_item'] = array( + 'label' => t('Field collection item'), + 'label callback' => 'entity_class_label', + 'uri callback' => 'entity_class_uri', + 'entity class' => 'FieldCollectionItemEntity', + 'controller class' => 'EntityAPIController', + 'base table' => 'field_collection_item', + 'revision table' => 'field_collection_item_revision', + 'fieldable' => TRUE, + // For integration with Redirect module. + // @see http://drupal.org/node/1263884 + 'redirect' => FALSE, + 'entity keys' => array( + 'id' => 'item_id', + 'revision' => 'revision_id', + 'bundle' => 'field_name', + ), + 'module' => 'field_collection', + 'view modes' => array( + 'full' => array( + 'label' => t('Full content'), + 'custom settings' => FALSE, + ), + ), + 'access callback' => 'field_collection_item_access', + 'deletion callback' => 'field_collection_item_delete', + 'metadata controller class' => 'FieldCollectionItemMetadataController' + ); + + // Add info about the bundles. We do not use field_info_fields() but directly + // use field_read_fields() as field_info_fields() requires built entity info + // to work. + foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) { + $return['field_collection_item']['bundles'][$field_name] = array( + 'label' => t('Field collection @field', array('@field' => $field_name)), + 'admin' => array( + 'path' => 'admin/structure/field-collections/%field_collection_field_name', + 'real path' => 'admin/structure/field-collections/' . strtr($field_name, array('_' => '-')), + 'bundle argument' => 3, + 'access arguments' => array('administer field collections'), + ), + ); + } + + if (module_exists('entitycache')) { + $return['field_collection_item']['field cache'] = FALSE; + $return['field_collection_item']['entity cache'] = TRUE; + } + + return $return; +} + +/** + * Menu callback for loading the bundle names. + */ +function field_collection_field_name_load($arg) { + $field_name = strtr($arg, array('-' => '_')); + if (($field = field_info_field($field_name)) && $field['type'] == 'field_collection') { + return $field_name; + } +} + +/** + * Loads a field collection item. + * + * @return field_collection_item + * The field collection item entity or FALSE. + */ +function field_collection_item_load($item_id, $reset = FALSE) { + $result = field_collection_item_load_multiple(array($item_id), array(), $reset); + return $result ? reset($result) : FALSE; +} + +/** + * Loads a field collection revision. + * + * @param $revision_id + * The field collection revision ID. + */ +function field_collection_item_revision_load($revision_id) { + return entity_revision_load('field_collection_item', $revision_id); +} + +/** + * Loads field collection items. + * + * @return + * An array of field collection item entities. + */ +function field_collection_item_load_multiple($ids = array(), $conditions = array(), $reset = FALSE) { + return entity_load('field_collection_item', $ids, $conditions, $reset); +} + +/** + * Class for field_collection_item entities. + */ +class FieldCollectionItemEntity extends Entity { + + /** + * Field collection field info. + * + * @var array + */ + protected $fieldInfo; + + /** + * The host entity object. + * + * @var object + */ + protected $hostEntity; + + /** + * The host entity ID. + * + * @var integer + */ + protected $hostEntityId; + + /** + * The host entity revision ID if this is not the default revision. + * + * @var integer + */ + protected $hostEntityRevisionId; + + /** + * The host entity type. + * + * @var string + */ + protected $hostEntityType; + + /** + * The language under which the field collection item is stored. + * + * @var string + */ + protected $langcode = LANGUAGE_NONE; + + /** + * Entity ID. + * + * @var integer + */ + public $item_id; + + /** + * Field collection revision ID. + * + * @var integer + */ + public $revision_id; + + /** + * The name of the field-collection field this item is associated with. + * + * @var string + */ + public $field_name; + + /** + * Whether this revision is the default revision. + * + * @var bool + */ + public $default_revision = TRUE; + + /** + * Whether the field collection item is archived, i.e. not in use. + * + * @see FieldCollectionItemEntity::isInUse() + * @var bool + */ + public $archived = FALSE; + + /** + * Constructs the entity object. + */ + public function __construct(array $values = array(), $entityType = NULL) { + parent::__construct($values, 'field_collection_item'); + // Workaround issues http://drupal.org/node/1084268 and + // http://drupal.org/node/1264440: + // Check if the required property is set before checking for the field's + // type. If the property is not set, we are hitting a PDO or a core's bug. + // FIXME: Remove when #1264440 is fixed and the required PHP version is + // properly identified and documented in the module documentation. + if (isset($this->field_name)) { + // Ok, we have the field name property, we can proceed and check the field's type + $field_info = $this->fieldInfo(); + if (!$field_info || $field_info['type'] != 'field_collection') { + throw new Exception("Invalid field name given: {$this->field_name} is not a Field Collection field."); + } + } + } + + /** + * Provides info about the field on the host entity, which embeds this + * field collection item. + */ + public function fieldInfo() { + return field_info_field($this->field_name); + } + + /** + * Provides info of the field instance containing the reference to this + * field collection item. + */ + public function instanceInfo() { + if ($this->fetchHostDetails()) { + return field_info_instance($this->hostEntityType(), $this->field_name, $this->hostEntityBundle()); + } + } + + /** + * Returns the field instance label translated to interface language. + */ + public function translatedInstanceLabel($langcode = NULL) { + if ($info = $this->instanceInfo()) { + if (module_exists('i18n_field')) { + return i18n_string("field:{$this->field_name}:{$info['bundle']}:label", $info['label'], array('langcode' => $langcode)); + } + return $info['label']; + } + } + + /** + * Specifies the default label, which is picked up by label() by default. + */ + public function defaultLabel() { + // @todo make configurable. + if ($this->fetchHostDetails()) { + $field = $this->fieldInfo(); + $label = $this->translatedInstanceLabel(); + + if ($field['cardinality'] == 1) { + return $label; + } + elseif ($this->item_id) { + return t('!instance_label @count', array('!instance_label' => $label, '@count' => $this->delta() + 1)); + } + else { + return t('New !instance_label', array('!instance_label' => $label)); + } + } + return t('Unconnected field collection item'); + } + + /** + * Returns the path used to view the entity. + */ + public function path() { + if ($this->item_id) { + return field_collection_field_get_path($this->fieldInfo()) . '/' . $this->item_id; + } + } + + /** + * Returns the URI as returned by entity_uri(). + */ + public function defaultUri() { + return array( + 'path' => $this->path(), + ); + } + + /** + * Sets the host entity. Only possible during creation of a item. + * + * @param $create_link + * (optional) Whether a field-item linking the host entity to the field + * collection item should be created. + */ + public function setHostEntity($entity_type, $entity, $langcode = LANGUAGE_NONE, $create_link = TRUE) { + if (!empty($this->is_new)) { + $this->hostEntityType = $entity_type; + $this->hostEntity = $entity; + $this->langcode = $langcode; + + list($this->hostEntityId, $this->hostEntityRevisionId) = entity_extract_ids($this->hostEntityType, $this->hostEntity); + // If the host entity is not saved yet, set the id to FALSE. So + // fetchHostDetails() does not try to load the host entity details. + if (!isset($this->hostEntityId)) { + $this->hostEntityId = FALSE; + } + // We are create a new field collection for a non-default entity, thus + // set archived to TRUE. + if (!entity_revision_is_default($entity_type, $entity)) { + $this->hostEntityId = FALSE; + $this->archived = TRUE; + } + if ($create_link) { + $entity->{$this->field_name}[$this->langcode][] = array('entity' => $this); + } + } + else { + throw new Exception('The host entity may be set only during creation of a field collection item.'); + } + } + + /** + * Updates the wrapped host entity object. + * + * @param $entity + */ + public function updateHostEntity($entity) { + $this->fetchHostDetails(); + list($recieved_id) = entity_extract_ids($this->hostEntityType, $entity); + + if ($this->isInUse()) { + $current_id = $this->hostEntityId; + } + else { + $current_host = entity_revision_load($this->hostEntityType, $this->hostEntityRevisionId); + list($current_id) = entity_extract_ids($this->hostEntityType, $current_host); + } + + if ($current_id == $recieved_id) { + $this->hostEntity = $entity; + $delta = $this->delta(); + if (isset($entity->{$this->field_name}[$this->langcode][$delta]['entity'])) { + $entity->{$this->field_name}[$this->langcode][$delta]['entity'] = $entity; + } + } + else { + throw new Exception('The host entity cannot be changed.'); + } + } + + /** + * Returns the host entity, which embeds this field collection item. + */ + public function hostEntity() { + if ($this->fetchHostDetails()) { + if (!isset($this->hostEntity) && $this->isInUse()) { + $this->hostEntity = entity_load_single($this->hostEntityType, $this->hostEntityId); + } + elseif (!isset($this->hostEntity) && $this->hostEntityRevisionId) { + $this->hostEntity = entity_revision_load($this->hostEntityType, $this->hostEntityRevisionId); + } + return $this->hostEntity; + } + } + + /** + * Returns the entity type of the host entity, which embeds this + * field collection item. + */ + public function hostEntityType() { + if ($this->fetchHostDetails()) { + return $this->hostEntityType; + } + } + + /** + * Returns the id of the host entity, which embeds this field collection item. + */ + public function hostEntityId() { + if ($this->fetchHostDetails()) { + if (!$this->hostEntityId && $this->hostEntityRevisionId) { + $this->hostEntityId = entity_id($this->hostEntityType, $this->hostEntity()); + } + return $this->hostEntityId; + } + } + + /** + * Returns the bundle of the host entity, which embeds this field collection + * item. + */ + public function hostEntityBundle() { + if ($entity = $this->hostEntity()) { + list($id, $rev_id, $bundle) = entity_extract_ids($this->hostEntityType, $entity); + return $bundle; + } + } + + protected function fetchHostDetails() { + if (!isset($this->hostEntityId)) { + if ($this->item_id) { + // For saved field collections, query the field data to determine the + // right host entity. + $query = new EntityFieldQuery(); + $query->fieldCondition($this->fieldInfo(), 'revision_id', $this->revision_id); + if (!$this->isInUse()) { + $query->age(FIELD_LOAD_REVISION); + } + $result = $query->execute(); + list($this->hostEntityType, $data) = each($result); + + if ($this->isInUse()) { + $this->hostEntityId = $data ? key($data) : FALSE; + $this->hostEntityRevisionId = FALSE; + } + // If we are querying for revisions, we get the revision ID. + else { + $this->hostEntityId = FALSE; + $this->hostEntityRevisionId = $data ? key($data) : FALSE; + } + } + else { + // No host entity available yet. + $this->hostEntityId = FALSE; + } + } + return !empty($this->hostEntityId) || !empty($this->hostEntity) || !empty($this->hostEntityRevisionId); + } + + /** + * Determines the $delta of the reference pointing to this field collection + * item. + */ + public function delta() { + if (($entity = $this->hostEntity()) && isset($entity->{$this->field_name})) { + foreach ($entity->{$this->field_name} as $langcode => &$data) { + foreach ($data as $delta => $item) { + if (isset($item['value']) && $item['value'] == $this->item_id) { + $this->langcode = $langcode; + return $delta; + } + elseif (isset($item['entity']) && $item['entity'] === $this) { + $this->langcode = $langcode; + return $delta; + } + } + } + } + } + + /** + * Determines the language code under which the item is stored. + */ + public function langcode() { + if ($this->delta() !== NULL) { + return $this->langcode; + } + } + + /** + * Determines whether this field collection item revision is in use. + * + * Field collection items may be contained in from non-default host entity + * revisions. If the field collection item does not appear in the default + * host entity revision, the item is actually not used by default and so + * marked as 'archived'. + * If the field collection item appears in the default revision of the host + * entity, the default revision of the field collection item is in use there + * and the collection is not marked as archived. + */ + public function isInUse() { + return $this->default_revision && !$this->archived; + } + + /** + * Save the field collection item. + * + * By default, always save the host entity, so modules are able to react + * upon changes to the content of the host and any 'last updated' dates of + * entities get updated. + * + * For creating an item a host entity has to be specified via setHostEntity() + * before this function is invoked. For the link between the entities to be + * fully established, the host entity object has to be updated to include a + * reference on this field collection item during saving. So do not skip + * saving the host for creating items. + * + * @param $skip_host_save + * (internal) If TRUE is passed, the host entity is not saved automatically + * and therefore no link is created between the host and the item or + * revision updates might be skipped. Use with care. + */ + public function save($skip_host_save = FALSE) { + // Make sure we have a host entity during creation. + if (!empty($this->is_new) && !(isset($this->hostEntityId) || isset($this->hostEntity) || isset($this->hostEntityRevisionId))) { + throw new Exception("Unable to create a field collection item without a given host entity."); + } + + // Only save directly if we are told to skip saving the host entity. Else, + // we always save via the host as saving the host might trigger saving + // field collection items anyway (e.g. if a new revision is created). + if ($skip_host_save) { + return entity_get_controller($this->entityType)->save($this); + } + else { + $host_entity = $this->hostEntity(); + if (!$host_entity) { + throw new Exception("Unable to save a field collection item without a valid reference to a host entity."); + } + // If this is creating a new revision, also do so for the host entity. + if (!empty($this->revision) || !empty($this->is_new_revision)) { + $host_entity->revision = TRUE; + if (!empty($this->default_revision)) { + entity_revision_set_default($this->hostEntityType, $host_entity); + } + } + // Set the host entity reference, so the item will be saved with the host. + // @see field_collection_field_presave() + $delta = $this->delta(); + if (isset($delta)) { + $host_entity->{$this->field_name}[$this->langcode][$delta] = array('entity' => $this); + } + else { + $host_entity->{$this->field_name}[$this->langcode][] = array('entity' => $this); + } + return entity_save($this->hostEntityType, $host_entity); + } + } + + /** + * Deletes the field collection item and the reference in the host entity. + */ + public function delete() { + parent::delete(); + $this->deleteHostEntityReference(); + } + + /** + * Deletes the host entity's reference of the field collection item. + */ + protected function deleteHostEntityReference() { + $delta = $this->delta(); + if ($this->item_id && isset($delta)) { + unset($this->hostEntity->{$this->field_name}[$this->langcode][$delta]); + entity_save($this->hostEntityType, $this->hostEntity); + } + } + + /** + * Intelligently delete a field collection item revision. + * + * If a host entity is revisioned with its field collection items, deleting + * a field collection item on the default revision of the host should not + * delete the collection item from archived revisions too. Instead, we delete + * the current default revision and archive the field collection. + * + */ + public function deleteRevision($skip_host_update = FALSE) { + if (!$this->revision_id) { + return; + } + + if (!$skip_host_update) { + // Just remove the item from the host, which cares about deleting the + // item (depending on whether the update creates a new revision). + $this->deleteHostEntityReference(); + } + + if (!$this->isDefaultRevision()) { + entity_revision_delete('field_collection_item', $this->revision_id); + } + // If deleting the default revision, take care! + else { + $row = db_select('field_collection_item_revision', 'r') + ->fields('r') + ->condition('item_id', $this->item_id) + ->condition('revision_id', $this->revision_id, '<>') + ->execute() + ->fetchAssoc(); + // If no other revision is left, delete. Else archive the item. + if (!$row) { + $this->delete(); + } + else { + // Make the other revision the default revision and archive the item. + db_update('field_collection_item') + ->fields(array('archived' => 1, 'revision_id' => $row['revision_id'])) + ->condition('item_id', $this->item_id) + ->execute(); + entity_get_controller('field_collection_item')->resetCache(array($this->item_id)); + entity_revision_delete('field_collection_item', $this->revision_id); + } + } + } + + /** + * Export the field collection item. + * + * Since field collection entities are not directly exportable (i.e., do not + * have 'exportable' set to TRUE in hook_entity_info()) and since Features + * calls this method when exporting the field collection as a field attached + * to another entity, we return the export in the format expected by + * Features, rather than in the normal Entity::export() format. + */ + public function export($prefix = '') { + // Based on code in EntityDefaultFeaturesController::export_render(). + $export = "entity_import('" . $this->entityType() . "', '"; + $export .= addcslashes(parent::export(), '\\\''); + $export .= "')"; + return $export; + } + + /** + * Magic method to only serialize what's necessary. + */ + public function __sleep() { + $vars = get_object_vars($this); + unset($vars['entityInfo'], $vars['idKey'], $vars['nameKey'], $vars['statusKey']); + unset($vars['fieldInfo']); + // Also do not serialize the host entity, but only if it has already an id. + if ($this->hostEntity && ($this->hostEntityId || $this->hostEntityRevisionId)) { + unset($vars['hostEntity']); + } + + // Also key the returned array with the variable names so the method may + // be easily overridden and customized. + return drupal_map_assoc(array_keys($vars)); + } + + /** + * Magic method to invoke setUp() on unserialization. + * + * @todo: Remove this once it appears in a released entity API module version. + */ + public function __wakeup() { + $this->setUp(); + } +} + +/** + * Implements hook_menu(). + */ +function field_collection_menu() { + $items = array(); + if (module_exists('field_ui')) { + $items['admin/structure/field-collections'] = array( + 'title' => 'Field collections', + 'description' => 'Manage fields on field collections.', + 'page callback' => 'field_collections_overview', + 'access arguments' => array('administer field collections'), + 'type' => MENU_NORMAL_ITEM, + 'file' => 'field_collection.admin.inc', + ); + } + + // Add menu paths for viewing/editing/deleting field collection items. + foreach (field_info_fields() as $field) { + if ($field['type'] == 'field_collection') { + $path = field_collection_field_get_path($field); + $count = count(explode('/', $path)); + + $items[$path . '/%field_collection_item'] = array( + 'page callback' => 'field_collection_item_page_view', + 'page arguments' => array($count), + 'access callback' => 'field_collection_item_access', + 'access arguments' => array('view', $count), + 'file' => 'field_collection.pages.inc', + ); + $items[$path . '/%field_collection_item/view'] = array( + 'title' => 'View', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => -10, + ); + $items[$path . '/%field_collection_item/edit'] = array( + 'page callback' => 'drupal_get_form', + 'page arguments' => array('field_collection_item_form', $count), + 'access callback' => 'field_collection_item_access', + 'access arguments' => array('update', $count), + 'title' => 'Edit', + 'type' => MENU_LOCAL_TASK, + 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, + 'file' => 'field_collection.pages.inc', + ); + $items[$path . '/%field_collection_item/delete'] = array( + 'page callback' => 'drupal_get_form', + 'page arguments' => array('field_collection_item_delete_confirm', $count), + 'access callback' => 'field_collection_item_access', + 'access arguments' => array('delete', $count), + 'title' => 'Delete', + 'type' => MENU_LOCAL_TASK, + 'context' => MENU_CONTEXT_INLINE, + 'file' => 'field_collection.pages.inc', + ); + // Add entity type and the entity id as additional arguments. + $items[$path . '/add/%/%'] = array( + 'page callback' => 'field_collection_item_add', + 'page arguments' => array($field['field_name'], $count + 1, $count + 2), + // The pace callback takes care of checking access itself. + 'access callback' => TRUE, + 'file' => 'field_collection.pages.inc', + ); + // Add menu items for dealing with revisions. + $items[$path . '/%field_collection_item/revisions/%field_collection_item_revision'] = array( + 'page callback' => 'field_collection_item_page_view', + 'page arguments' => array($count + 2), + 'access callback' => 'field_collection_item_access', + 'access arguments' => array('view', $count + 2), + 'file' => 'field_collection.pages.inc', + ); + } + } + + $items['field_collection/ajax'] = array( + 'title' => 'Remove item callback', + 'page callback' => 'field_collection_remove_js', + 'delivery callback' => 'ajax_deliver', + 'access callback' => TRUE, + 'theme callback' => 'ajax_base_page_theme', + 'type' => MENU_CALLBACK, + 'file path' => 'includes', + 'file' => 'form.inc', + ); + + return $items; +} + +/** + * Implements hook_menu_alter() to fix the field collections admin UI tabs. + */ +function field_collection_menu_alter(&$items) { + if (module_exists('field_ui') && isset($items['admin/structure/field-collections/%field_collection_field_name/fields'])) { + // Make the fields task the default local task. + $items['admin/structure/field-collections/%field_collection_field_name'] = $items['admin/structure/field-collections/%field_collection_field_name/fields']; + $item = &$items['admin/structure/field-collections/%field_collection_field_name']; + $item['type'] = MENU_NORMAL_ITEM; + $item['title'] = 'Manage fields'; + $item['title callback'] = 'field_collection_admin_page_title'; + $item['title arguments'] = array(3); + + $items['admin/structure/field-collections/%field_collection_field_name/fields'] = array( + 'title' => 'Manage fields', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => 1, + ); + } +} + +/** + * Menu title callback. + */ +function field_collection_admin_page_title($field_name) { + return t('Field collection @field_name', array('@field_name' => $field_name)); +} + +/** + * Implements hook_admin_paths(). + */ +function field_collection_admin_paths() { + if (variable_get('node_admin_theme')) { + return array( + 'field-collection/*/*/edit' => TRUE, + 'field-collection/*/*/delete' => TRUE, + 'field-collection/*/add/*/*' => TRUE, + ); + } +} + +/** + * Implements hook_permission(). + */ +function field_collection_permission() { + return array( + 'administer field collections' => array( + 'title' => t('Administer field collections'), + 'description' => t('Create and delete fields on field collections.'), + ), + ); +} + +/** + * Determines whether the given user has access to a field collection. + * + * @param $op + * The operation being performed. One of 'view', 'update', 'create', 'delete'. + * @param $item + * Optionally a field collection item. If nothing is given, access for all + * items is determined. + * @param $account + * The user to check for. Leave it to NULL to check for the global user. + * @return boolean + * Whether access is allowed or not. + */ +function field_collection_item_access($op, FieldCollectionItemEntity $item = NULL, $account = NULL) { + // We do not support editing field collection revisions that are not used at + // the hosts default revision as saving the host might result in a new default + // revision. + if (isset($item) && !$item->isInUse() && $op != 'view') { + return FALSE; + } + if (user_access('administer field collections', $account)) { + return TRUE; + } + if (!isset($item)) { + return FALSE; + } + $op = $op == 'view' ? 'view' : 'edit'; + // Access is determined by the entity and field containing the reference. + $field = field_info_field($item->field_name); + $entity_access = entity_access($op == 'view' ? 'view' : 'update', $item->hostEntityType(), $item->hostEntity(), $account); + return $entity_access && field_access($op, $field, $item->hostEntityType(), $item->hostEntity(), $account); +} + +/** + * Deletion callback + */ +function field_collection_item_delete($id) { + $fci = field_collection_item_load($id); + $fci->delete(); +} + +/** + * Implements hook_theme(). + */ +function field_collection_theme() { + return array( + 'field_collection_item' => array( + 'render element' => 'elements', + 'template' => 'field-collection-item', + ), + 'field_collection_view' => array( + 'render element' => 'element', + ), + ); +} + +/** + * Implements hook_field_info(). + */ +function field_collection_field_info() { + return array( + 'field_collection' => array( + 'label' => t('Field collection'), + 'description' => t('This field stores references to embedded entities, which itself may contain any number of fields.'), + 'instance_settings' => array(), + 'default_widget' => 'field_collection_hidden', + 'default_formatter' => 'field_collection_view', + // As of now there is no UI for setting the path. + 'settings' => array( + 'path' => '', + 'hide_blank_items' => TRUE, + ), + // Add entity property info. + 'property_type' => 'field_collection_item', + 'property_callbacks' => array('field_collection_entity_metadata_property_callback'), + ), + ); +} + +/** + * Implements hook_field_instance_settings_form(). + */ +function field_collection_field_instance_settings_form($field, $instance) { + + $element['fieldset'] = array( + '#type' => 'fieldset', + '#title' => t('Default value'), + '#collapsible' => FALSE, + // As field_ui_default_value_widget() does, we change the #parents so that + // the value below is writing to $instance in the right location. + '#parents' => array('instance'), + ); + // Be sure to set the default value to NULL, e.g. to repair old fields + // that still have one. + $element['fieldset']['default_value'] = array( + '#type' => 'value', + '#value' => NULL, + ); + $element['fieldset']['content'] = array( + '#pre' => '

', + '#markup' => t('To specify a default value, configure it via the regular default value setting of each field that is part of the field collection. To do so, go to the Manage fields screen of the field collection.', array('!url' => url('admin/structure/field-collections/' . strtr($field['field_name'], array('_' => '-')) . '/fields'))), + '#suffix' => '

', + ); + return $element; +} + +/** + * Returns the base path to use for field collection items. + */ +function field_collection_field_get_path($field) { + if (empty($field['settings']['path'])) { + return 'field-collection/' . strtr($field['field_name'], array('_' => '-')); + } + return $field['settings']['path']; +} + +/** + * Implements hook_field_settings_form(). + */ +function field_collection_field_settings_form($field, $instance) { + + $form['hide_blank_items'] = array( + '#type' => 'checkbox', + '#title' => t('Hide blank items'), + '#default_value' => $field['settings']['hide_blank_items'], + '#description' => t("A blank item is always added to any unlimited valued field's form. If checked, any additional blank items are hidden except of the first item which is always shown."), + '#weight' => 10, + '#states' => array( + // Show the setting if the cardinality is -1. + 'visible' => array( + ':input[name="field[cardinality]"]' => array('value' => '-1'), + ), + ), + ); + return $form; +} + +/** + * Implements hook_field_insert(). + */ +function field_collection_field_insert($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) { + foreach ($items as &$item) { + if ($entity = field_collection_field_get_entity($item)) { + if (!empty($host_entity->is_new) && empty($entity->is_new)) { + // If the host entity is new but we have a field_collection that is not + // new, it means that its host is being cloned. Thus we need to clone + // the field collection entity as well. + $new_entity = clone $entity; + $new_entity->item_id = NULL; + $new_entity->revision_id = NULL; + $new_entity->is_new = TRUE; + $entity = $new_entity; + } + if (!empty($entity->is_new)) { + $entity->setHostEntity($host_entity_type, $host_entity, LANGUAGE_NONE, FALSE); + } + $entity->save(TRUE); + $item = array( + 'value' => $entity->item_id, + 'revision_id' => $entity->revision_id, + ); + } + } +} + +/** + * Implements hook_field_update(). + * + * Care about removed field collection items. + * Support saving field collection items in @code $item['entity'] @endcode. This + * may be used to seamlessly create field collection items during host-entity + * creation or to save changes to the host entity and its collections at once. + */ +function field_collection_field_update($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) { + // Prevent workbench moderation from deleting field collections on node_save() + // during workbench_moderation_store(), when $host_entity->revision == 0. + if (!empty($host_entity->workbench_moderation['updating_live_revision'])) { + return; + } + + $items_original = !empty($host_entity->original->{$field['field_name']}[$langcode]) ? $host_entity->original->{$field['field_name']}[$langcode] : array(); + $original_by_id = array_flip(field_collection_field_item_to_ids($items_original)); + + foreach ($items as &$item) { + // In case the entity has been changed / created, save it and set the id. + // If the host entity creates a new revision, save new item-revisions as + // well. + if (isset($item['entity']) || !empty($host_entity->revision)) { + + if ($entity = field_collection_field_get_entity($item)) { + + if (!empty($entity->is_new)) { + $entity->setHostEntity($host_entity_type, $host_entity, LANGUAGE_NONE, FALSE); + } + + // If the host entity is saved as new revision, do the same for the item. + if (!empty($host_entity->revision)) { + $entity->revision = TRUE; + // Without this cache clear entity_revision_is_default will + // incorrectly return false here when creating a new published revision + if (!isset($cleared_host_entity_cache)) { + list($entity_id) = entity_extract_ids($host_entity_type, $host_entity); + entity_get_controller($host_entity_type)->resetCache(array($entity_id)); + $cleared_host_entity_cache = true; + } + $is_default = entity_revision_is_default($host_entity_type, $host_entity); + // If an entity type does not support saving non-default entities, + // assume it will be saved as default. + if (!isset($is_default) || $is_default) { + $entity->default_revision = TRUE; + $entity->archived = FALSE; + } + } + $entity->save(TRUE); + + $item = array( + 'value' => $entity->item_id, + 'revision_id' => $entity->revision_id, + ); + } + } + unset($original_by_id[$item['value']]); + } + + // If there are removed items, care about deleting the item entities. + if ($original_by_id) { + $ids = array_flip($original_by_id); + + // If we are creating a new revision, the old-items should be kept but get + // marked as archived now. + if (!empty($host_entity->revision)) { + db_update('field_collection_item') + ->fields(array('archived' => 1)) + ->condition('item_id', $ids, 'IN') + ->execute(); + } + else { + // Delete unused field collection items now. + foreach (field_collection_item_load_multiple($ids) as $un_item) { + $un_item->updateHostEntity($host_entity); + $un_item->deleteRevision(TRUE); + } + } + } +} + +/** + * Implements hook_field_delete(). + */ +function field_collection_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) { + $ids = field_collection_field_item_to_ids($items); + // Also delete all embedded entities. + if ($ids && field_info_field($field['field_name'])) { + // We filter out entities that are still being referenced by other + // host-entities. This should never be the case, but it might happened e.g. + // when modules cloned a node without knowing about field-collection. + $entity_info = entity_get_info($entity_type); + $entity_id_name = $entity_info['entity keys']['id']; + $field_column = key($field['columns']); + + foreach ($ids as $id_key => $id) { + $query = new EntityFieldQuery(); + $entities = $query + ->fieldCondition($field['field_name'], $field_column, $id) + ->execute(); + unset($entities[$entity_type][$entity->$entity_id_name]); + + if (!empty($entities[$entity_type])) { + // Filter this $id out. + unset($ids[$id_key]); + } + } + + entity_delete_multiple('field_collection_item', $ids); + } +} + +/** + * Implements hook_field_delete_revision(). + */ +function field_collection_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) { + foreach ($items as $item) { + if (!empty($item['revision_id'])) { + if ($entity = field_collection_item_revision_load($item['revision_id'])) { + $entity->deleteRevision(TRUE); + } + } + } +} + +/** + * Get an array of field collection item IDs stored in the given field items. + */ +function field_collection_field_item_to_ids($items) { + $ids = array(); + foreach ($items as $item) { + if (!empty($item['value'])) { + $ids[] = $item['value']; + } + } + return $ids; +} + +/** + * Implements hook_field_is_empty(). + */ +function field_collection_field_is_empty($item, $field) { + if (!empty($item['value'])) { + return FALSE; + } + elseif (isset($item['entity'])) { + return field_collection_item_is_empty($item['entity']); + } + return TRUE; +} + +/** + * Determines whether a field collection item entity is empty based on the collection-fields. + */ +function field_collection_item_is_empty(FieldCollectionItemEntity $item) { + $instances = field_info_instances('field_collection_item', $item->field_name); + $is_empty = TRUE; + + foreach ($instances as $instance) { + $field_name = $instance['field_name']; + $field = field_info_field($field_name); + + // Determine the list of languages to iterate on. + $languages = field_available_languages('field_collection_item', $field); + + foreach ($languages as $langcode) { + if (!empty($item->{$field_name}[$langcode])) { + // If at least one collection-field is not empty; the + // field collection item is not empty. + foreach ($item->{$field_name}[$langcode] as $field_item) { + if (!module_invoke($field['module'], 'field_is_empty', $field_item, $field)) { + $is_empty = FALSE; + } + } + } + } + } + + // Allow other modules a chance to alter the value before returning. + drupal_alter('field_collection_is_empty', $is_empty, $item); + return $is_empty; +} + +/** + * Implements hook_field_formatter_info(). + */ +function field_collection_field_formatter_info() { + return array( + 'field_collection_list' => array( + 'label' => t('Links to field collection items'), + 'field types' => array('field_collection'), + 'settings' => array( + 'edit' => t('Edit'), + 'delete' => t('Delete'), + 'add' => t('Add'), + 'description' => TRUE, + ), + ), + 'field_collection_view' => array( + 'label' => t('Field collection items'), + 'field types' => array('field_collection'), + 'settings' => array( + 'edit' => t('Edit'), + 'delete' => t('Delete'), + 'add' => t('Add'), + 'description' => TRUE, + 'view_mode' => 'full', + ), + ), + 'field_collection_fields' => array( + 'label' => t('Fields only'), + 'field types' => array('field_collection'), + 'settings' => array( + 'view_mode' => 'full', + ), + ), + ); +} + +/** + * Implements hook_field_formatter_settings_form(). + */ +function field_collection_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + $elements = array(); + + if ($display['type'] != 'field_collection_fields') { + $elements['edit'] = array( + '#type' => 'textfield', + '#title' => t('Edit link title'), + '#default_value' => $settings['edit'], + '#description' => t('Leave the title empty, to hide the link.'), + ); + $elements['delete'] = array( + '#type' => 'textfield', + '#title' => t('Delete link title'), + '#default_value' => $settings['delete'], + '#description' => t('Leave the title empty, to hide the link.'), + ); + $elements['add'] = array( + '#type' => 'textfield', + '#title' => t('Add link title'), + '#default_value' => $settings['add'], + '#description' => t('Leave the title empty, to hide the link.'), + ); + $elements['description'] = array( + '#type' => 'checkbox', + '#title' => t('Show the field description beside the add link.'), + '#default_value' => $settings['description'], + '#description' => t('If enabled and the add link is shown, the field description is shown in front of the add link.'), + ); + } + + // Add a select form element for view_mode if viewing the rendered field_collection. + if ($display['type'] !== 'field_collection_list') { + + $entity_type = entity_get_info('field_collection_item'); + $options = array(); + foreach ($entity_type['view modes'] as $mode => $info) { + $options[$mode] = $info['label']; + } + + $elements['view_mode'] = array( + '#type' => 'select', + '#title' => t('View mode'), + '#options' => $options, + '#default_value' => $settings['view_mode'], + '#description' => t('Select the view mode'), + ); + } + + return $elements; +} + +/** + * Implements hook_field_formatter_settings_summary(). + */ +function field_collection_field_formatter_settings_summary($field, $instance, $view_mode) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + $output = array(); + + if ($display['type'] !== 'field_collection_fields') { + $links = array_filter(array_intersect_key($settings, array_flip(array('add', 'edit', 'delete')))); + if ($links) { + $output[] = t('Links: @links', array('@links' => check_plain(implode(', ', $links)))); + } + else { + $output[] = t('Links: none'); + } + } + + if ($display['type'] !== 'field_collection_list') { + $entity_type = entity_get_info('field_collection_item'); + if (!empty($entity_type['view modes'][$settings['view_mode']]['label'])) { + $output[] = t('View mode: @mode', array('@mode' => $entity_type['view modes'][$settings['view_mode']]['label'])); + } + } + + return implode('
', $output); +} + +/** + * Implements hook_field_formatter_view(). + */ +function field_collection_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $element = array(); + $settings = $display['settings']; + + switch ($display['type']) { + case 'field_collection_list': + + foreach ($items as $delta => $item) { + if ($field_collection = field_collection_field_get_entity($item)) { + $output = l($field_collection->label(), $field_collection->path()); + $links = array(); + foreach (array('edit', 'delete') as $op) { + if ($settings[$op] && field_collection_item_access($op == 'edit' ? 'update' : $op, $field_collection)) { + $title = entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_$op", $settings[$op]); + $links[] = l($title, $field_collection->path() . '/' . $op, array('query' => drupal_get_destination())); + } + } + if ($links) { + $output .= ' (' . implode('|', $links) . ')'; + } + $element[$delta] = array('#markup' => $output); + } + } + field_collection_field_formatter_links($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display); + break; + + case 'field_collection_view': + + $view_mode = !empty($display['settings']['view_mode']) ? $display['settings']['view_mode'] : 'full'; + foreach ($items as $delta => $item) { + if ($field_collection = field_collection_field_get_entity($item)) { + $element[$delta]['entity'] = $field_collection->view($view_mode); + $element[$delta]['#theme_wrappers'] = array('field_collection_view'); + $element[$delta]['#attributes']['class'][] = 'field-collection-view'; + $element[$delta]['#attributes']['class'][] = 'clearfix'; + $element[$delta]['#attributes']['class'][] = drupal_clean_css_identifier('view-mode-' . $view_mode); + + $links = array( + '#theme' => 'links__field_collection_view', + ); + $links['#attributes']['class'][] = 'field-collection-view-links'; + foreach (array('edit', 'delete') as $op) { + if ($settings[$op] && field_collection_item_access($op == 'edit' ? 'update' : $op, $field_collection)) { + $links['#links'][$op] = array( + 'title' => entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_$op", $settings[$op]), + 'href' => $field_collection->path() . '/' . $op, + 'query' => drupal_get_destination(), + ); + } + } + $element[$delta]['links'] = $links; + } + } + field_collection_field_formatter_links($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display); + if (!empty($items) || !empty($element['#suffix'])) { + $element['#attached']['css'][] = drupal_get_path('module', 'field_collection') . '/field_collection.theme.css'; + } + break; + + case 'field_collection_fields': + + $view_mode = !empty($display['settings']['view_mode']) ? $display['settings']['view_mode'] : 'full'; + foreach ($items as $delta => $item) { + if ($field_collection = field_collection_field_get_entity($item)) { + $element[$delta]['entity'] = $field_collection->view($view_mode); + } + } + break; + } + + return $element; +} + +/** + * Helper function to add links to a field collection field. + */ +function field_collection_field_formatter_links(&$element, $entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $settings = $display['settings']; + $allow_create_item = FALSE; + + if ($settings['add'] && ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || count($items) < $field['cardinality'])) { + // Check whether the current is allowed to create a new item. + $field_collection_item = entity_create('field_collection_item', array('field_name' => $field['field_name'])); + $field_collection_item->setHostEntity($entity_type, $entity, LANGUAGE_NONE, FALSE); + + if (field_collection_item_access('create', $field_collection_item)) { + $allow_create_item = TRUE; + $path = field_collection_field_get_path($field); + list($id) = entity_extract_ids($entity_type, $entity); + $element['#suffix'] = ''; + if (!empty($settings['description'])) { + $element['#suffix'] .= '
' . field_filter_xss($instance['description']) . '
'; + } + $title = entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_add", $settings['add']); + $add_path = $path . '/add/' . $entity_type . '/' . $id; + $element['#suffix'] .= ''; + } + } + // If there is no add link, add a special class to the last item. + if (!empty($items) || $allow_create_item) { + if (empty($element['#suffix'])) { + $index = count(element_children($element)) - 1; + $element[$index]['#attributes']['class'][] = 'field-collection-view-final'; + } + + $element += array('#prefix' => '', '#suffix' => ''); + $element['#prefix'] .= '
'; + $element['#suffix'] .= '
'; + } + + return $element; +} + +/** + * Themes field collection items printed using the field_collection_view formatter. + */ +function theme_field_collection_view($variables) { + $element = $variables['element']; + return '' . $element['#children'] . ''; +} + +/** + * Implements hook_field_widget_info(). + */ +function field_collection_field_widget_info() { + return array( + 'field_collection_hidden' => array( + 'label' => t('Hidden'), + 'field types' => array('field_collection'), + 'behaviors' => array( + 'multiple values' => FIELD_BEHAVIOR_CUSTOM, + 'default value' => FIELD_BEHAVIOR_NONE, + ), + ), + 'field_collection_embed' => array( + 'label' => t('Embedded'), + 'field types' => array('field_collection'), + 'behaviors' => array( + 'multiple values' => FIELD_BEHAVIOR_DEFAULT, + 'default value' => FIELD_BEHAVIOR_NONE, + ), + ), + ); +} + +/** + * Implements hook_field_widget_form(). + */ +function field_collection_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + static $recursion = 0; + + switch ($instance['widget']['type']) { + case 'field_collection_hidden': + return $element; + + case 'field_collection_embed': + // If the field collection item form contains another field collection, + // we might ran into a recursive loop. Prevent that. + if ($recursion++ > 3) { + drupal_set_message(t('The field collection item form has not been embedded to avoid recursive loops.'), 'error'); + return $element; + } + $field_parents = $element['#field_parents']; + $field_name = $element['#field_name']; + $language = $element['#language']; + + // Nest the field collection item entity form in a dedicated parent space, + // by appending [field_name, langcode, delta] to the current parent space. + // That way the form values of the field collection item are separated. + $parents = array_merge($field_parents, array($field_name, $language, $delta)); + + $element += array( + '#element_validate' => array('field_collection_field_widget_embed_validate'), + '#parents' => $parents, + ); + + if ($field['cardinality'] == 1) { + $element['#type'] = 'fieldset'; + } + + $field_state = field_form_get_state($field_parents, $field_name, $language, $form_state); + + if (field_collection_hide_blank_items($field) && $delta == $field_state['items_count'] && $delta > 0) { + // Do not add a blank item. Also see + // field_collection_field_attach_form() for correcting #max_delta. + $recursion--; + return FALSE; + } + elseif (field_collection_hide_blank_items($field) && $field_state['items_count'] == 0) { + // We show one item, so also specify that as item count. So when the + // add button is pressed the item count will be 2 and we show to items. + $field_state['items_count'] = 1; + } + + if (isset($field_state['entity'][$delta])) { + $field_collection_item = $field_state['entity'][$delta]; + } + else { + if (isset($items[$delta])) { + $field_collection_item = field_collection_field_get_entity($items[$delta], $field_name); + } + // Show an empty collection if we have no existing one or it does not + // load. + if (empty($field_collection_item)) { + $field_collection_item = entity_create('field_collection_item', array('field_name' => $field_name)); + $field_collection_item->setHostEntity($element['#entity_type'], $element['#entity']); + } + + // Put our entity in the form state, so FAPI callbacks can access it. + $field_state['entity'][$delta] = $field_collection_item; + } + + field_form_set_state($field_parents, $field_name, $language, $form_state, $field_state); + field_attach_form('field_collection_item', $field_collection_item, $element, $form_state, $language); + + if (empty($element['#required'])) { + $element['#after_build'][] = 'field_collection_field_widget_embed_delay_required_validation'; + } + + if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) { + $element['remove_button'] = array( + '#delta' => $delta, + '#name' => implode('_', $parents) . '_remove_button', + '#type' => 'submit', + '#value' => t('Remove'), + '#validate' => array(), + '#submit' => array('field_collection_remove_submit'), + '#limit_validation_errors' => array(), + '#ajax' => array( + 'path' => 'field_collection/ajax', + 'effect' => 'fade', + ), + '#weight' => 1000, + ); + } + + $recursion--; + return $element; + } +} + +/** + * Implements hook_field_attach_form(). + * + * Corrects #max_delta when we hide the blank field collection item. + * + * @see field_add_more_js() + * @see field_collection_field_widget_form() + */ +function field_collection_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { + + foreach (field_info_instances($entity_type, $form['#bundle']) as $field_name => $instance) { + $field = field_info_field($field_name); + + if ($field['type'] == 'field_collection' && field_collection_hide_blank_items($field) + && field_access('edit', $field, $entity_type) && $instance['widget']['type'] == 'field_collection_embed') { + + $element_langcode = $form[$field_name]['#language']; + if ($form[$field_name][$element_langcode]['#max_delta'] > 0) { + $form[$field_name][$element_langcode]['#max_delta']--; + } + } + } +} + +/** + * Page callback to handle AJAX for removing a field collection item. + * + * This is a direct page callback. The actual job of deleting the item is + * done in the submit handler for the button, so all we really need to + * do is process the form and then generate output. We generate this + * output by doing a replace command on the id of the entire form element. + */ +function field_collection_remove_js() { + // drupal_html_id() very helpfully ensures that all html IDS are unique + // on a page. Unfortunately what it doesn't realize is that the IDs + // we are generating are going to replace IDs that already exist, so + // this actually works against us. + if (isset($_POST['ajax_html_ids'])) { + unset($_POST['ajax_html_ids']); + } + + list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form(); + drupal_process_form($form['#form_id'], $form, $form_state); + + // Get the information on what we're removing. + $button = $form_state['triggering_element']; + // Go two levels up in the form, to the whole widget. + $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -3)); + // Now send back the proper AJAX command to replace it. + $commands[] = ajax_command_replace('#' . $element['#id'], drupal_render($element)); + $return = array( + '#type' => 'ajax', + '#commands' => $commands, + ); + + // Because we're doing this ourselves, messages aren't automatic. We have + // to add them. + $messages = theme('status_messages'); + if ($messages) { + $return['#commands'][] = ajax_command_prepend('#' . $element['#id'], $messages); + } + + return $return; +} + +/** + * Submit callback to remove an item from the field UI multiple wrapper. + * + * When a remove button is submitted, we need to find the item that it + * referenced and delete it. Since field UI has the deltas as a straight + * unbroken array key, we have to renumber everything down. Since we do this + * we *also* need to move all the deltas around in the $form_state['values'], + * $form_state['input'], and $form_state['field'] so that user changed values + * follow. This is a bit of a complicated process. + */ +function field_collection_remove_submit($form, &$form_state) { + $button = $form_state['triggering_element']; + $delta = $button['#delta']; + + // Where in the form we'll find the parent element. + $address = array_slice($button['#array_parents'], 0, -2); + + // Go one level up in the form, to the widgets container. + $parent_element = drupal_array_get_nested_value($form, $address); + $field_name = $parent_element['#field_name']; + $langcode = $parent_element['#language']; + $parents = $parent_element['#field_parents']; + + $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state); + + // Go ahead and renumber everything from our delta to the last + // item down one. This will overwrite the item being removed. + for ($i = $delta; $i <= $field_state['items_count']; $i++) { + $old_element_address = array_merge($address, array($i + 1)); + $new_element_address = array_merge($address, array($i)); + + $moving_element = drupal_array_get_nested_value($form, $old_element_address); + $moving_element_value = drupal_array_get_nested_value($form_state['values'], $old_element_address); + $moving_element_input = drupal_array_get_nested_value($form_state['input'], $old_element_address); + $moving_element_field = drupal_array_get_nested_value($form_state['field']['#parents'], $old_element_address); + + // Tell the element where it's being moved to. + $moving_element['#parents'] = $new_element_address; + + // Move the element around. + form_set_value($moving_element, $moving_element_value, $form_state); + drupal_array_set_nested_value($form_state['input'], $moving_element['#parents'], $moving_element_input); + drupal_array_set_nested_value($form_state['field']['#parents'], $moving_element['#parents'], $moving_element_field); + + // Move the entity in our saved state. + if (isset($field_state['entity'][$i + 1])) { + $field_state['entity'][$i] = $field_state['entity'][$i + 1]; + } + else { + unset($field_state['entity'][$i]); + } + } + + // Replace the deleted entity with an empty one. This helps to ensure that + // trying to add a new entity won't ressurect a deleted entity from the + // trash bin. + $count = count($field_state['entity']); + $field_state['entity'][$count] = entity_create('field_collection_item', array('field_name' => $field_name)); + + // Then remove the last item. But we must not go negative. + if ($field_state['items_count'] > 0) { + $field_state['items_count']--; + } + + // Fix the weights. Field UI lets the weights be in a range of + // (-1 * item_count) to (item_count). This means that when we remove one, + // the range shrinks; weights outside of that range then get set to + // the first item in the select by the browser, floating them to the top. + // We use a brute force method because we lost weights on both ends + // and if the user has moved things around, we have to cascade because + // if I have items weight weights 3 and 4, and I change 4 to 3 but leave + // the 3, the order of the two 3s now is undefined and may not match what + // the user had selected. + $input = drupal_array_get_nested_value($form_state['input'], $address); + // Sort by weight + uasort($input, '_field_sort_items_helper'); + + // Reweight everything in the correct order. + $weight = -1 * $field_state['items_count']; + foreach ($input as $key => $item) { + if ($item) { + $input[$key]['_weight'] = $weight++; + } + } + + drupal_array_set_nested_value($form_state['input'], $address, $input); + field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state); + + $form_state['rebuild'] = TRUE; +} + +/** + * Gets a field collection item entity for a given field item. + * + * @param $field_name + * (optional) If given and there is no entity yet, a new entity object is + * created for the given item. + * + * @return + * The entity object or FALSE. + */ +function field_collection_field_get_entity(&$item, $field_name = NULL) { + if (isset($item['entity'])) { + return $item['entity']; + } + elseif (isset($item['value'])) { + // By default always load the default revision, so caches get used. + $entity = field_collection_item_load($item['value']); + if ($entity->revision_id != $item['revision_id']) { + // A non-default revision is a referenced, so load this one. + $entity = field_collection_item_revision_load($item['revision_id']); + } + return $entity; + } + elseif (!isset($item['entity']) && isset($field_name)) { + $item['entity'] = entity_create('field_collection_item', array('field_name' => $field_name)); + return $item['entity']; + } + return FALSE; +} + +/** + * FAPI #after_build of an individual field collection element to delay the validation of #required. + */ +function field_collection_field_widget_embed_delay_required_validation(&$element, &$form_state) { + // If the process_input flag is set, the form and its input is going to be + // validated. Prevent #required (sub)fields from throwing errors while + // their non-#required field collection item is empty. + if ($form_state['process_input']) { + _field_collection_collect_required_elements($element, $element['#field_collection_required_elements']); + } + return $element; +} + +function _field_collection_collect_required_elements(&$element, &$required_elements) { + // Recurse through all children. + foreach (element_children($element) as $key) { + if (isset($element[$key]) && $element[$key]) { + _field_collection_collect_required_elements($element[$key], $required_elements); + } + } + if (!empty($element['#required'])) { + $element['#required'] = FALSE; + $required_elements[] = &$element; + $element += array('#pre_render' => array()); + array_unshift($element['#pre_render'], 'field_collection_field_widget_render_required'); + } +} + +/** + * #pre_render callback that ensures the element is rendered as being required. + */ +function field_collection_field_widget_render_required($element) { + $element['#required'] = TRUE; + return $element; +} + +/** + * FAPI validation of an individual field collection element. + */ +function field_collection_field_widget_embed_validate($element, &$form_state, $complete_form) { + $instance = field_widget_instance($element, $form_state); + $field = field_widget_field($element, $form_state); + $field_parents = $element['#field_parents']; + $field_name = $element['#field_name']; + $language = $element['#language']; + + $field_state = field_form_get_state($field_parents, $field_name, $language, $form_state); + $field_collection_item = $field_state['entity'][$element['#delta']]; + + // Attach field API validation of the embedded form. + field_attach_form_validate('field_collection_item', $field_collection_item, $element, $form_state); + + // Now validate required elements if the entity is not empty. + if (!field_collection_item_is_empty($field_collection_item) && !empty($element['#field_collection_required_elements'])) { + foreach ($element['#field_collection_required_elements'] as &$elements) { + + // Copied from _form_validate(). + // #1676206: Modified to support options widget. + if (isset($elements['#needs_validation'])) { + $is_empty_multiple = (!count($elements['#value'])); + $is_empty_string = (is_string($elements['#value']) && drupal_strlen(trim($elements['#value'])) == 0); + $is_empty_value = ($elements['#value'] === 0); + $is_empty_option = (isset($elements['#options']['_none']) && $elements['#value'] == '_none'); + if ($is_empty_multiple || $is_empty_string || $is_empty_value || $is_empty_option) { + if (isset($elements['#title'])) { + form_error($elements, t('@name field is required.', array('@name' => $elements['#title']))); + } + else { + form_error($elements); + } + } + } + } + } + + // Only if the form is being submitted, finish the collection entity and + // prepare it for saving. + if ($form_state['submitted'] && !form_get_errors()) { + + field_attach_submit('field_collection_item', $field_collection_item, $element, $form_state); + + // Load initial form values into $item, so any other form values below the + // same parents are kept. + $item = drupal_array_get_nested_value($form_state['values'], $element['#parents']); + + // Set the _weight if it is a multiple field. + if (isset($element['_weight']) && ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED)) { + $item['_weight'] = $element['_weight']['#value']; + } + + // Put the field collection item in $item['entity'], so it is saved with + // the host entity via hook_field_presave() / field API if it is not empty. + // @see field_collection_field_presave() + $item['entity'] = $field_collection_item; + form_set_value($element, $item, $form_state); + } +} + +/** + * Implements hook_field_create_field(). + */ +function field_collection_field_create_field($field) { + if ($field['type'] == 'field_collection') { + field_attach_create_bundle('field_collection_item', $field['field_name']); + + // Clear caches. + entity_info_cache_clear(); + // Do not directly issue menu rebuilds here to avoid potentially multiple + // rebuilds. Instead, let menu_get_item() issue the rebuild on the next + // request. + variable_set('menu_rebuild_needed', TRUE); + } +} + +/** + * Implements hook_field_delete_field(). + */ +function field_collection_field_delete_field($field) { + if ($field['type'] == 'field_collection') { + // Notify field.module that field collection was deleted. + field_attach_delete_bundle('field_collection_item', $field['field_name']); + + // Clear caches. + entity_info_cache_clear(); + // Do not directly issue menu rebuilds here to avoid potentially multiple + // rebuilds. Instead, let menu_get_item() issue the rebuild on the next + // request. + variable_set('menu_rebuild_needed', TRUE); + } +} + +/** + * Implements hook_i18n_string_list_{textgroup}_alter(). + */ +function field_collection_i18n_string_list_field_alter(&$properties, $type, $instance) { + if ($type == 'field_instance') { + $field = field_info_field($instance['field_name']); + + if ($field['type'] == 'field_collection' && !empty($instance['display'])) { + + foreach ($instance['display'] as $view_mode => $display) { + if ($display['type'] != 'field_collection_fields') { + $display['settings'] += array('edit' => 'edit', 'delete' => 'delete', 'add' => 'add'); + + $properties['field'][$instance['field_name']][$instance['bundle']]['setting_edit'] = array( + 'title' => t('Edit link title'), + 'string' => $display['settings']['edit'], + ); + $properties['field'][$instance['field_name']][$instance['bundle']]['setting_delete'] = array( + 'title' => t('Delete link title'), + 'string' => $display['settings']['delete'], + ); + $properties['field'][$instance['field_name']][$instance['bundle']]['setting_add'] = array( + 'title' => t('Add link title'), + 'string' => $display['settings']['add'], + ); + } + } + } + } +} + +/** + * Implements hook_views_api(). + */ +function field_collection_views_api() { + return array( + 'api' => '3.0-alpha1', + 'path' => drupal_get_path('module', 'field_collection') . '/views', + ); +} + +/** + * Implements hook_features_pipe_COMPONENT_alter() for field objects. + * + * This is used with Features v1.0 and v2.0 prior to beta2, newer releases + * separated the field_base from the field_instance so this won't be used. + * + * @see field_collection_features_pipe_field_instance_alter(). + */ +function field_collection_features_pipe_field_alter(&$pipe, $data, $export) { + // Skip this if Features has been updated to v2.0-beta2 or newer as it will + // use the separate field_instance integration instead. + if (!function_exists('field_instance_features_export_options')) { + // Add the fields of the field collection entity to the pipe. + foreach ($data as $identifier) { + if (($field = features_field_load($identifier)) && $field['field_config']['type'] == 'field_collection') { + $fields = field_info_instances('field_collection_item', $field['field_config']['field_name']); + foreach ($fields as $name => $field) { + $pipe['field'][] = "{$field['entity_type']}-{$field['bundle']}-{$field['field_name']}"; + } + } + } + } +} + +/** + * Implements hook_features_pipe_COMPONENT_alter() for field_instance objects. + * + * This is used with Features v2.0-beta2 and newer. + */ +function field_collection_features_pipe_field_instance_alter(&$pipe, $data, $export) { + // Add the fields of the field collection entity to the pipe. + foreach ($data as $identifier) { + if (($field = features_field_load($identifier)) && $field['field_config']['type'] == 'field_collection') { + $fields = field_info_instances('field_collection_item', $field['field_config']['field_name']); + foreach ($fields as $name => $field) { + $pipe['field_instance'][] = "{$field['entity_type']}-{$field['bundle']}-{$field['field_name']}"; + } + } + } +} + +/** + * Callback for generating entity metadata property info for our field instances. + * + * @see field_collection_field_info() + */ +function field_collection_entity_metadata_property_callback(&$info, $entity_type, $field, $instance, $field_type) { + $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']]; + // Set the bundle as we know it is the name of the field. + $property['bundle'] = $field['field_name']; + $property['getter callback'] = 'field_collection_field_property_get'; +} + +/** + * Entity property info setter callback for the host entity property. + * + * As the property is of type entity, the value will be passed as a wrapped + * entity. + */ +function field_collection_item_set_host_entity($item, $property_name, $wrapper) { + if (empty($item->is_new)) { + throw new EntityMetadataWrapperException('The host entity may be set only during creation of a field collection item.'); + } + if (!isset($wrapper->{$item->field_name})) { + throw new EntityMetadataWrapperException('The specified entity has no such field collection field.'); + } + $item->setHostEntity($wrapper->type(), $wrapper->value()); +} + +/** + * Entity property info getter callback for the host entity property. + */ +function field_collection_item_get_host_entity($item) { + // As the property is defined as 'entity', we have to return a wrapped entity. + return entity_metadata_wrapper($item->hostEntityType(), $item->hostEntity()); +} + +/** + * Entity property info getter callback for the field collection items. + * + * Like entity_metadata_field_property_get(), but additionally supports getting + * not-yet saved collection items from @code $item['entity'] @endcode. + */ +function field_collection_field_property_get($entity, array $options, $name, $entity_type, $info) { + $field = field_info_field($name); + $langcode = field_language($entity_type, $entity, $name, isset($options['language']) ? $options['language']->language : NULL); + $values = array(); + if (isset($entity->{$name}[$langcode])) { + foreach ($entity->{$name}[$langcode] as $delta => $data) { + // Wrappers do not support multiple entity references being revisions or + // not yet saved entities. In the case of a single reference we can return + // the entity object though. + if ($field['cardinality'] == 1) { + $values[$delta] = field_collection_field_get_entity($data); + } + elseif (isset($data['value'])) { + $values[$delta] = $data['value']; + } + } + } + // For an empty single-valued field, we have to return NULL. + return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values; +} + +/** + * Implements hook_devel_generate(). + */ +function field_collection_devel_generate($object, $field, $instance, $bundle) { + // Create a new field collection object and add fake data to its fields. + $field_collection = entity_create('field_collection_item', array('field_name' => $field['field_name'])); + $field_collection->language = $object->language; + $field_collection->setHostEntity($instance['entity_type'], $object, $object->language, FALSE); + + devel_generate_fields($field_collection, 'field_collection_item', $field['field_name']); + + $field_collection->save(TRUE); + + return array( + 'value' => $field_collection->item_id, + 'revision_id' => $field_collection->revision_id, + ); +} + +/** + * Determines if the additional blank items should be displayed or not. + * + * @param array $field + * The field info array. + * + * @return bool + * TRUE if the additional blank items should be hidden, and FALSE if not. + */ +function field_collection_hide_blank_items($field) { + return !empty($field['settings']['hide_blank_items']) && $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED; +} diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.pages.inc b/sites/all/modules/contrib/fields/field_collection/field_collection.pages.inc new file mode 100644 index 0000000..b88e9ed --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.pages.inc @@ -0,0 +1,139 @@ +label()); + return $field_collection_item->view('full', NULL, TRUE); +} + +/** + * Form for editing a field collection item. + * @todo implement hook_forms(). + */ +function field_collection_item_form($form, &$form_state, $field_collection_item) { + if (!isset($field_collection_item->is_new)) { + drupal_set_title($field_collection_item->label()); + } + $form_state += array('field_collection_item' => $field_collection_item); + + // Hack: entity_form_field_validate() needs the bundle to be set. + // @todo: Fix core and remove the hack. + $form['field_name'] = array('#type' => 'value', '#value' => $field_collection_item->field_name); + + field_attach_form('field_collection_item', $field_collection_item, $form, $form_state); + + $form['actions'] = array('#type' => 'actions', '#weight' => 50); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + '#weight' => 5, + ); + return $form; +} + +/** + * Validation callback. + */ +function field_collection_item_form_validate($form, &$form_state) { + entity_form_field_validate('field_collection_item', $form, $form_state); +} + +/** + * Submit builder. Extracts the form values and updates the entity. + */ +function field_collection_item_form_submit_build_field_collection($form, $form_state) { + entity_form_submit_build_entity('field_collection_item', $form_state['field_collection_item'], $form, $form_state); + return $form_state['field_collection_item']; +} + +/** + * Submit callback that permanently saves the changes to the entity. + */ +function field_collection_item_form_submit($form, &$form_state) { + $field_collection_item = field_collection_item_form_submit_build_field_collection($form, $form_state); + $field_collection_item->save(); + drupal_set_message(t('The changes have been saved.')); + $form_state['redirect'] = $field_collection_item->path(); +} + +/** + * Form for deleting a field collection item. + */ +function field_collection_item_delete_confirm($form, &$form_state, $field_collection_item) { + $form_state += array('field_collection_item' => $field_collection_item); + return confirm_form($form, + t('Are you sure you want to delete %label?', array('%label' => $field_collection_item->label())), + $field_collection_item->path(), + t('This action cannot be undone.'), + t('Delete'), + t('Cancel') + ); +} + +/** + * Submit callback for deleting a field collection item. + */ +function field_collection_item_delete_confirm_submit($form, &$form_state) { + $field_collection_item = $form_state['field_collection_item']; + $field_collection_item->deleteRevision(); + drupal_set_message(t('%label has been deleted.', array('%label' => drupal_ucfirst($field_collection_item->label())))); + $form_state['redirect'] = ''; +} + +/** + * Add a new field collection item. + * + * @todo: Support optionally passing in the revision_id and langcode parameters. + */ +function field_collection_item_add($field_name, $entity_type, $entity_id, $revision_id = NULL, $langcode = NULL) { + $info = entity_get_info(); + if (!isset($info[$entity_type])) { + return MENU_NOT_FOUND; + } + $result = entity_load($entity_type, array($entity_id)); + $entity = reset($result); + if (!$entity) { + return MENU_NOT_FOUND; + } + // Ensure the given entity is of a bundle that has an instance of the field. + list($id, $rev_id, $bundle) = entity_extract_ids($entity_type, $entity); + $instance = field_info_instance($entity_type, $field_name, $bundle); + if (!$instance) { + return MENU_NOT_FOUND; + } + + // Check field cardinality. + $field = field_info_field($field_name); + $langcode = LANGUAGE_NONE; + if (!($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || !isset($entity->{$field_name}[$langcode]) || count($entity->{$field_name}[$langcode]) < $field['cardinality'])) { + drupal_set_message(t('Too many items.'), 'error'); + return ''; + } + + $field_collection_item = entity_create('field_collection_item', array('field_name' => $field_name)); + // Do not link the field collection item with the host entity at this point, + // as during the form-workflow we have multiple field collection item entity + // instances, which we don't want link all with the host. + // That way the link is going to be created when the item is saved. + $field_collection_item->setHostEntity($entity_type, $entity, LANGUAGE_NONE, FALSE); + + $label = $field_collection_item->translatedInstanceLabel(); + $title = ($field['cardinality'] == 1) ? $label : t('Add new !instance_label', array('!instance_label' => $label)); + drupal_set_title($title); + + // Make sure the current user has access to create a field collection item. + if (!field_collection_item_access('create', $field_collection_item)) { + return MENU_ACCESS_DENIED; + } + return drupal_get_form('field_collection_item_form', $field_collection_item); +} diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.test b/sites/all/modules/contrib/fields/field_collection/field_collection.test new file mode 100644 index 0000000..a2fd734 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.test @@ -0,0 +1,580 @@ + 'Field collection', + 'description' => 'Tests creating and using field collections.', + 'group' => 'Field types', + ); + } + + function setUp() { + parent::setUp('field_collection'); + + // Create a field_collection field to use for the tests. + $this->field_name = 'field_test_collection'; + $this->field = array('field_name' => $this->field_name, 'type' => 'field_collection', 'cardinality' => 4); + $this->field = field_create_field($this->field); + $this->field_id = $this->field['id']; + + $this->instance = array( + 'field_name' => $this->field_name, + 'entity_type' => 'node', + 'bundle' => 'article', + 'label' => $this->randomName() . '_label', + 'description' => $this->randomName() . '_description', + 'weight' => mt_rand(0, 127), + 'settings' => array(), + 'widget' => array( + 'type' => 'hidden', + 'label' => 'Test', + 'settings' => array(), + ), + ); + $this->instance = field_create_instance($this->instance); + } + + /** + * Helper for creating a new node with a field collection item. + */ + protected function createNodeWithFieldCollection() { + $node = $this->drupalCreateNode(array('type' => 'article')); + // Manually create a field_collection. + $entity = entity_create('field_collection_item', array('field_name' => $this->field_name)); + $entity->setHostEntity('node', $node); + $entity->save(); + + return array($node, $entity); + } + + /** + * Tests CRUD. + */ + function testCRUD() { + list ($node, $entity) = $this->createNodeWithFieldCollection(); + $node = node_load($node->nid, NULL, TRUE); + $this->assertEqual($entity->item_id, $node->{$this->field_name}[LANGUAGE_NONE][0]['value'], 'A field_collection has been successfully created and referenced.'); + $this->assertEqual($entity->revision_id, $node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id'], 'A field_collection has been successfully created and referenced.'); + + // Test adding an additional field_collection during node edit. + $entity2 = entity_create('field_collection_item', array('field_name' => $this->field_name)); + $node->{$this->field_name}[LANGUAGE_NONE][] = array('entity' => $entity2); + node_save($node); + + $node = node_load($node->nid, NULL, TRUE); + $this->assertTrue(!empty($entity2->item_id) && !empty($entity2->revision_id), 'Field_collection has been saved.'); + $this->assertEqual($entity->item_id, $node->{$this->field_name}[LANGUAGE_NONE][0]['value'], 'Existing reference has been kept during update.'); + $this->assertEqual($entity->revision_id, $node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id'], 'Existing reference has been kept during update (revision).'); + $this->assertEqual($entity2->item_id, $node->{$this->field_name}[LANGUAGE_NONE][1]['value'], 'New field_collection has been properly referenced'); + $this->assertEqual($entity2->revision_id, $node->{$this->field_name}[LANGUAGE_NONE][1]['revision_id'], 'New field_collection has been properly referenced (revision)'); + + // Make sure deleting the field_collection removes the reference. + $entity2->delete(); + $node = node_load($node->nid, NULL, TRUE); + $this->assertTrue(!isset($node->{$this->field_name}[LANGUAGE_NONE][1]), 'Reference correctly deleted.'); + + // Make sure field_collections are removed during deletion of the host. + node_delete($node->nid); + $this->assertTrue(entity_load('field_collection_item', FALSE) === array(), 'Field collections are deleted when the host is deleted.'); + + // Try deleting nodes with collections without any values. + $node = $this->drupalCreateNode(array('type' => 'article')); + node_delete($node->nid); + $this->assertTrue(node_load($node->nid, NULL, TRUE) == FALSE, 'Node without collection values deleted.'); + + // Test creating a field collection entity with a not-yet saved host entity. + $node = entity_create('node', array('type' => 'article')); + $entity = entity_create('field_collection_item', array('field_name' => $this->field_name)); + $entity->setHostEntity('node', $node); + $entity->save(); + // Now the node should have been saved with the collection and the link + // should have been established. + $this->assertTrue(!empty($node->nid), 'Node has been saved with the collection.'); + $this->assertTrue(count($node->{$this->field_name}[LANGUAGE_NONE]) == 1 && !empty($node->{$this->field_name}[LANGUAGE_NONE][0]['value']) && !empty($node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']), 'Link has been established.'); + + // Again, test creating a field collection with a not-yet saved host entity, + // but this time save both entities via the host. + $node = entity_create('node', array('type' => 'article')); + $entity = entity_create('field_collection_item', array('field_name' => $this->field_name)); + $entity->setHostEntity('node', $node); + node_save($node); + $this->assertTrue(!empty($entity->item_id) && !empty($entity->revision_id), 'Collection has been saved with the host.'); + $this->assertTrue(count($node->{$this->field_name}[LANGUAGE_NONE]) == 1 && !empty($node->{$this->field_name}[LANGUAGE_NONE][0]['value']) && !empty($node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']), 'Link has been established.'); + + // Test Revisions. + list ($node, $item) = $this->createNodeWithFieldCollection(); + $entity2 = entity_create('field_collection_item', array('field_name' => $this->field_name)); + $node->{$this->field_name}[LANGUAGE_NONE][] = array('entity' => $entity2); + node_save($node); + $this->assertEqual($entity2->archived, FALSE, 'New field collection item with new content revision is not archived.'); + + // Test saving a new revision of a node. + $node->revision = TRUE; + node_save($node); + $item_updated = field_collection_item_load($node->{$this->field_name}[LANGUAGE_NONE][0]['value']); + $this->assertNotEqual($item->revision_id, $item_updated->revision_id, 'Creating a new host entity revision creates a new field collection revision.'); + + // Test saving a new revision with a new field collection item. + $node->revision = TRUE; + + // Test saving the node without creating a new revision. + $item = $item_updated; + $node->revision = FALSE; + node_save($node); + $item_updated = field_collection_item_load($node->{$this->field_name}[LANGUAGE_NONE][0]['value']); + $this->assertEqual($item->revision_id, $item_updated->revision_id, 'Updating a new host entity without creating a new revision does not create a new field collection revision.'); + + // Create a new revision of the node, such we have a non default node and + // field collection revision. Then test using it. + $vid = $node->vid; + $item_revision_id = $item_updated->revision_id; + $node->revision = TRUE; + node_save($node); + + $item_updated = field_collection_item_load($node->{$this->field_name}[LANGUAGE_NONE][0]['value']); + $this->assertNotEqual($item_revision_id, $item_updated->revision_id, 'Creating a new host entity revision creates a new field collection revision.'); + $this->assertTrue($item_updated->isDefaultRevision(), 'Field collection of default host entity revision is default too.'); + $this->assertEqual($item_updated->hostEntityId(), $node->nid, 'Can access host entity ID of default field collection revision.'); + $this->assertEqual($item_updated->hostEntity()->vid, $node->vid, 'Loaded default host entity revision.'); + + $item = entity_revision_load('field_collection_item', $item_revision_id); + $this->assertFalse($item->isDefaultRevision(), 'Field collection of non-default host entity is non-default too.'); + $this->assertEqual($item->hostEntityId(), $node->nid, 'Can access host entity ID of non-default field collection revision.'); + $this->assertEqual($item->hostEntity()->vid, $vid, 'Loaded non-default host entity revision.'); + + // Delete the non-default revision and make sure the field collection item + // revision has been deleted too. + entity_revision_delete('node', $vid); + $this->assertFalse(entity_revision_load('node', $vid), 'Host entity revision deleted.'); + $this->assertFalse(entity_revision_load('field_collection_item', $item_revision_id), 'Field collection item revision deleted.'); + + // Test having archived field collections, i.e. collections referenced only + // in non-default revisions. + list ($node, $item) = $this->createNodeWithFieldCollection(); + // Create two revisions. + $node_vid = $node->vid; + $node->revision = TRUE; + node_save($node); + + $node_vid2 = $node->vid; + $node->revision = TRUE; + node_save($node); + + // Now delete the field collection item for the default revision. + $item = field_collection_item_load($node->{$this->field_name}[LANGUAGE_NONE][0]['value']); + $item_revision_id = $item->revision_id; + $item->deleteRevision(); + $node = node_load($node->nid); + $this->assertTrue(!isset($node->{$this->field_name}[LANGUAGE_NONE][0]), 'Field collection item revision removed from host.'); + $this->assertFalse(field_collection_item_revision_load($item->revision_id), 'Field collection item default revision deleted.'); + + $item = field_collection_item_load($item->item_id); + $this->assertNotEqual($item->revision_id, $item_revision_id, 'Field collection default revision has been updated.'); + $this->assertTrue($item->archived, 'Field collection item has been archived.'); + $this->assertFalse($item->isInUse(), 'Field collection item specified as not in use.'); + $this->assertTrue($item->isDefaultRevision(), 'Field collection of non-default host entity is default (but archived).'); + $this->assertEqual($item->hostEntityId(), $node->nid, 'Can access host entity ID of non-default field collection revision.'); + $this->assertEqual($item->hostEntity()->nid, $node->nid, 'Loaded non-default host entity revision.'); + + // Test deleting a revision of an archived field collection. + $node_revision2 = node_load($node->nid, $node_vid2); + $item = field_collection_item_revision_load($node_revision2->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']); + $item->deleteRevision(); + // There should be one revision left, so the item should still exist. + $item = field_collection_item_load($item->item_id); + $this->assertTrue($item->archived, 'Field collection item is still archived.'); + $this->assertFalse($item->isInUse(), 'Field collection item specified as not in use.'); + + // Test that deleting the first node revision deletes the whole field + // collection item as it contains its last revision. + node_revision_delete($node_vid); + $this->assertFalse(field_collection_item_load($item->item_id), 'Archived field collection deleted when last revision deleted.'); + + // Test that removing a field-collection item also deletes it. + list ($node, $item) = $this->createNodeWithFieldCollection(); + + $node->{$this->field_name}[LANGUAGE_NONE] = array(); + $node->revision = FALSE; + node_save($node); + $this->assertFalse(field_collection_item_load($item->item_id), 'Removed field collection item has been deleted.'); + + // Test removing a field-collection item while creating a new host revision. + list ($node, $item) = $this->createNodeWithFieldCollection(); + $node->{$this->field_name}[LANGUAGE_NONE] = array(); + $node->revision = TRUE; + node_save($node); + // Item should not be deleted but archived now. + $item = field_collection_item_load($item->item_id); + $this->assertTrue($item, 'Removed field collection item still exists.'); + $this->assertTrue($item->archived, 'Removed field collection item is archived.'); + + // Test removing an old node revision. Make sure that the field collection + // is not removed + list ($node, $item) = $this->createNodeWithFieldCollection(); + $node_vid = $node->vid; + $node->revision = TRUE; + node_save($node); + $node_vid2 = $node->vid; + $item_vid2 = $node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']; + node_revision_delete($node_vid); + $item2 = field_collection_item_revision_load($item_vid2); + $item_id2 = isset($item2->item_id) ? $item2->item_id : -1; + $this->assertEqual($item_id2, $item->item_id, 'Removing an old node revision does not delete newer field collection revisions'); + + } + + /** + * Make sure the basic UI and access checks are working. + */ + function testBasicUI() { + // Add a field to the collection. + $field = array( + 'field_name' => 'field_text', + 'type' => 'text', + 'cardinality' => 1, + 'translatable' => FALSE, + ); + field_create_field($field); + $instance = array( + 'entity_type' => 'field_collection_item', + 'field_name' => 'field_text', + 'bundle' => $this->field_name, + 'label' => 'Test text field', + 'widget' => array( + 'type' => 'text_textfield', + ), + ); + field_create_instance($instance); + + $user = $this->drupalCreateUser(); + $node = $this->drupalCreateNode(array('type' => 'article')); + + $this->drupalLogin($user); + // Make sure access is denied. + $path = 'field-collection/field-test-collection/add/node/' . $node->nid; + $this->drupalGet($path); + $this->assertText(t('Access denied'), 'Access has been denied.'); + + $user_privileged = $this->drupalCreateUser(array('access content', 'edit any article content')); + $this->drupalLogin($user_privileged); + $this->drupalGet("node/$node->nid"); + $this->assertLinkByHref($path, 0, 'Add link is shown.'); + $this->drupalGet($path); + $this->assertText(t('Test text field'), 'Add form is shown.'); + + $edit['field_text[und][0][value]'] = $this->randomName(); + $this->drupalPost($path, $edit, t('Save')); + $this->assertText(t('The changes have been saved.'), 'Field collection saved.'); + + $this->assertText($edit['field_text[und][0][value]'], "Added field value is shown."); + + $edit['field_text[und][0][value]'] = $this->randomName(); + $this->drupalPost('field-collection/field-test-collection/1/edit', $edit, t('Save')); + $this->assertText(t('The changes have been saved.'), 'Field collection saved.'); + $this->assertText($edit['field_text[und][0][value]'], "Field collection has been edited."); + + $this->drupalGet('field-collection/field-test-collection/1'); + $this->assertText($edit['field_text[und][0][value]'], "Field collection can be viewed."); + + // Add further 3 items, so we have reached 4 == maxium cardinality. + $this->drupalPost($path, $edit, t('Save')); + $this->drupalPost($path, $edit, t('Save')); + $this->drupalPost($path, $edit, t('Save')); + // Make sure adding doesn't work any more as we have restricted cardinality + // to 1. + $this->drupalGet($path); + $this->assertText(t('Too many items.'), 'Maxium cardinality has been reached.'); + + $this->drupalPost('field-collection/field-test-collection/1/delete', array(), t('Delete')); + $this->drupalGet($path); + // Add form is shown again. + $this->assertText(t('Test text field'), 'Field collection item has been deleted.'); + + // Test the viewing a revision. There should be no links to change it. + $vid = $node->vid; + $node = node_load($node->nid, NULL, TRUE); + $node->revision = TRUE; + node_save($node); + + $this->drupalGet("node/$node->nid/revisions/$vid/view"); + $this->assertResponse(403, 'Access to view revision denied'); + // Login in as admin and try again. + $user = $this->drupalCreateUser(array('administer nodes', 'bypass node access')); + $this->drupalLogin($user); + $this->drupalGet("node/$node->nid/revisions/$vid/view"); + $this->assertNoResponse(403, 'Access to view revision granted'); + + $this->assertNoLinkByHref($path, 'No links on revision view.'); + $this->assertNoLinkByHref('field-collection/field-test-collection/2/edit', 'No links on revision view.'); + $this->assertNoLinkByHref('field-collection/field-test-collection/2/delete', 'No links on revision view.'); + + $this->drupalGet("node/$node->nid/revisions"); + } + + /** + * Make sure that field_collection-entities are copied when host-entities do. + */ + public function testCopyingEntities() { + list($node, $entity) = $this->createNodeWithFieldCollection(); + + // Create a copy of that node. + $node->nid = NULL; + $node->vid = NULL; + $node->is_new = TRUE; + + node_save($node); + $item = $node->{$this->field_name}[LANGUAGE_NONE][0]; + $this->assertNotEqual($entity->item_id, $item['value']); + + // Do a php clone to the $node object and save it. + $node2 = clone $node; + $node2->nid = NULL; + $node2->is_new = TRUE; + $node2->vid = NULL; + node_save($node2); + + $item2 = $node2->{$this->field_name}[LANGUAGE_NONE][0]; + $this->assertNotEqual($item2['value'], $item['value']); + + // Create another copy this time (needlessly) forcing a new revision. + $node->nid = NULL; + $node->vid = NULL; + $node->is_new = TRUE; + $node->revision = TRUE; + node_save($node); + $item3 = $node->{$this->field_name}[LANGUAGE_NONE][0]; + $this->assertNotEqual($item['value'], $item3['value']); + } + +} + + +/** + * Test using field collection with Rules. + */ +class FieldCollectionRulesIntegrationTestCase extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Field collection Rules integration', + 'description' => 'Tests using field collections with rules.', + 'group' => 'Field types', + 'dependencies' => array('rules'), + ); + } + + function setUp() { + parent::setUp(array('field_collection', 'rules')); + variable_set('rules_debug_log', 1); + } + + protected function createFields($cardinality = 4) { + // Create a field_collection field to use for the tests. + $this->field_name = 'field_test_collection'; + $this->field = array('field_name' => $this->field_name, 'type' => 'field_collection', 'cardinality' => $cardinality); + $this->field = field_create_field($this->field); + $this->field_id = $this->field['id']; + + $this->instance = array( + 'field_name' => $this->field_name, + 'entity_type' => 'node', + 'bundle' => 'article', + 'label' => $this->randomName() . '_label', + 'description' => $this->randomName() . '_description', + 'weight' => mt_rand(0, 127), + 'settings' => array(), + 'widget' => array( + 'type' => 'hidden', + 'label' => 'Test', + 'settings' => array(), + ), + ); + $this->instance = field_create_instance($this->instance); + // Add a field to the collection. + $field = array( + 'field_name' => 'field_text', + 'type' => 'text', + 'cardinality' => 1, + 'translatable' => FALSE, + ); + field_create_field($field); + $instance = array( + 'entity_type' => 'field_collection_item', + 'field_name' => 'field_text', + 'bundle' => $this->field_name, + 'label' => 'Test text field', + 'widget' => array( + 'type' => 'text_textfield', + ), + ); + field_create_instance($instance); + } + + /** + * Test creation field collection items. + */ + function testCreation() { + $this->createFields(); + + $node = $this->drupalCreateNode(array('type' => 'article')); + // Create a field collection. + $action_set = rules_action_set(array('node' => array('type' => 'node', 'bundle' => 'article'))); + $action_set->action('entity_create', array( + 'type' => 'field_collection_item', + 'param_field_name' => $this->field_name, + 'param_host_entity:select' => 'node', + )); + $action_set->action('data_set', array('data:select' => 'entity-created:field-text', 'value' => 'foo')); + $action_set->execute($node); + + $node = node_load($node->nid, NULL, TRUE); + $this->assertTrue(!empty($node->{$this->field_name}[LANGUAGE_NONE][0]['value']), 'A field_collection has been successfully created.'); + $this->assertTrue(!empty($node->{$this->field_name}[LANGUAGE_NONE][0]['revision_id']), 'A field_collection has been successfully created (revision).'); + + // Now try making use of the field collection in rules. + $action_set = rules_action_set(array('node' => array('type' => 'node', 'bundle' => 'article'))); + $action_set->action('drupal_message', array('message:select' => 'node:field-test-collection:0:field-text')); + $action_set->execute($node); + + $msg = drupal_get_messages(); + $this->assertEqual(array_pop($msg['status']), 'foo', 'Field collection can be used.'); + RulesLog::logger()->checkLog(); + } + + /** + * Test using field collection items via the host while they are being created. + */ + function testUsageDuringCreation() { + // Test using a single-cardinality field collection. + $this->createFields(1); + + $node = $this->drupalCreateNode(array('type' => 'article')); + $entity = entity_create('field_collection_item', array('field_name' => $this->field_name)); + $entity->setHostEntity('node', $node); + // Now the field collection is linked to the host, but not yet saved. + + // Test using the wrapper on it. + $wrapper = entity_metadata_wrapper('node', $node); + $wrapper->get($this->field_name)->field_text->set('foo'); + $this->assertEqual($entity->field_text[LANGUAGE_NONE][0]['value'], 'foo', 'Field collection item used during creation via the wrapper.'); + + // Now test it via Rules, which should save our changes. + $set = rules_action_set(array('node' => array('type' => 'node', 'bundle' => 'article'))); + $set->action('data_set', array('data:select' => 'node:' . $this->field_name . ':field-text', 'value' => 'bar')); + $set->execute($node); + $this->assertEqual($entity->field_text[LANGUAGE_NONE][0]['value'], 'bar', 'Field collection item used during creation via Rules.'); + $this->assertTrue(!empty($entity->item_id) && !empty($entity->revision_id), 'Field collection item has been saved by Rules and the host entity.'); + RulesLog::logger()->checkLog(); + } +} + +/** + * Test using field collection with content that gets translated. + */ +class FieldCollectionContentTranslationTestCase extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Field collection content translation', + 'description' => 'Tests using content under translation.', + 'group' => 'Field types', + 'dependencies' => array('translation'), + ); + } + + public function setUp() { + parent::setUp(array('field_collection', 'translation')); + // Create a field_collection field to use for the tests. + $this->field_name = 'field_test_collection'; + $this->field = array('field_name' => $this->field_name, 'type' => 'field_collection', 'cardinality' => 4); + $this->field = field_create_field($this->field); + $this->field_id = $this->field['id']; + + $this->instance = array( + 'field_name' => $this->field_name, + 'entity_type' => 'node', + 'bundle' => 'article', + 'label' => $this->randomName() . '_label', + 'description' => $this->randomName() . '_description', + 'weight' => mt_rand(0, 127), + 'settings' => array(), + 'widget' => array( + 'type' => 'field_collection_embed', + 'label' => 'Test', + 'settings' => array(), + ), + ); + $this->instance = field_create_instance($this->instance); + + // Add a field to the collection. + $field = array( + 'field_name' => 'field_text', + 'type' => 'text', + 'cardinality' => 1, + 'translatable' => FALSE, + ); + field_create_field($field); + $instance = array( + 'entity_type' => 'field_collection_item', + 'field_name' => 'field_text', + 'bundle' => $this->field_name, + 'label' => 'Test text field', + 'widget' => array( + 'type' => 'text_textfield', + ), + ); + field_create_instance($instance); + + $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages', 'create article content', 'edit any article content', 'translate content')); + + $this->drupalLogin($admin_user); + // Add German language. + locale_add_language('de'); + + // Set "Article" content type to use multilingual support. + variable_set('language_content_type_article', TRANSLATION_ENABLED); + } + + /** + * Ensure field collections are cloned to new entities on content translation. + */ + public function testContentTranslation() { + // Create "Article" content. + $edit['title'] = $this->randomName(); + $edit['body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName(); + $edit['language'] = 'en'; + $field_collection_name = 'field_test_collection[' . LANGUAGE_NONE . '][0][field_text][' . LANGUAGE_NONE . '][0][value]'; + $edit[$field_collection_name] = $this->randomName(); + + $this->drupalPost('node/add/article', $edit, t('Save')); + $this->assertRaw(t('Article %title has been created.', array('%title' => $edit['title'])), 'Article created.'); + $node1 = $this->drupalGetNodeByTitle($edit['title']); + + $this->drupalGet('node/' . $node1->nid . '/edit'); + $this->drupalGet('node/' . $node1->nid . '/translate'); + $this->drupalGet('node/add/article', array('query' => array('translation' => $node1->nid, 'target' => 'de'))); + + // Suffix translations with the langcode. + unset($edit['language']); + $edit['title'] .= 'DE'; + $edit[$field_collection_name] .= 'DE'; + $this->drupalPost('node/add/article', $edit, t('Save'), array('query' => array('translation' => $node1->nid, 'target' => 'de'))); + $node2 = $this->drupalGetNodeByTitle($edit['title']); + + // Ensure that our new node is the translation of the first one. + $this->assertEqual($node1->nid, $node2->tnid, 'Succesfully created translation.'); + // And check to see that their field collections are different. + $this->assertNotEqual($node1->field_test_collection, $node2->field_test_collection, 'Field collections between translation source and translation differ.'); + } + +} diff --git a/sites/all/modules/contrib/fields/field_collection/field_collection.theme.css b/sites/all/modules/contrib/fields/field_collection/field_collection.theme.css new file mode 100644 index 0000000..b619185 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/field_collection.theme.css @@ -0,0 +1,66 @@ +@CHARSET "UTF-8"; + +.field-collection-container { + border-bottom: 1px solid #D3D7D9; + margin-bottom: 1em; +} + +.field-collection-container .field-items .field-item { + margin-bottom: 10px; +} + +.field-collection-container .field-items .field-items .field-item { + margin-bottom: 0; +} + +.field-collection-view { + padding: 1em 0 0.3em 0; + margin: 0 1em 0 1em; + border-bottom: 1px dotted #D3D7D9; +} + +/* If there is no add link, don't show the final border. */ +.field-collection-view-final { + border-bottom: none; +} + +.field-collection-view .entity-field-collection-item { + float: left; +} + +.field-collection-view ul.field-collection-view-links { + float: right; + font-size: 0.821em; + list-style-type: none; + width: auto; + margin: 0 1em; + padding: 0; +} + +.field-collection-view .field-label { + width: 25%; +} + +.field-collection-view .content { + margin-top: 0; + width: 100%; +} + +.field-collection-view .entity-field-collection-item { + width: 100%; +} + +ul.field-collection-view-links li { + float: left; +} + +ul.field-collection-view-links li a { + margin-right: 1em; +} + +.field-collection-container ul.action-links-field-collection-add { + float: right; + padding: 0 0.5em 0 0; + margin: 0 0 1em 2em; + font-size: 0.821em; +} diff --git a/sites/all/modules/contrib/fields/field_collection/views/field_collection.views.inc b/sites/all/modules/contrib/fields/field_collection/views/field_collection.views.inc new file mode 100644 index 0000000..4774f74 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/views/field_collection.views.inc @@ -0,0 +1,60 @@ + 'field_collection_handler_relationship', + 'base' => 'field_collection_item', + 'base field' => 'item_id', + 'label' => t('field collection item from !field_name', array('!field_name' => $field['field_name'])), + 'field_name' => $field['field_name'], + ); + $data['field_revision_' . $field_name][$field_name . '_revision_id']['relationship'] = array( + 'handler' => 'field_collection_handler_relationship', + 'base' => 'field_collection_item_revision', + 'base field' => 'revision_id', + 'label' => t('field collection item revision from !field_name', array('!field_name' => $field['field_name'])), + 'field_name' => $field['field_name'], + ); + } + + foreach ($field['bundles'] as $entity_type => $bundles) { + $entity_info = entity_get_info($entity_type); + $pseudo_field_name = $field['field_name'] . '_' . $entity_type; + + list($label, $all_labels) = field_views_field_label($field['field_name']); + $entity = $entity_info['label']; + if ($entity == t('Node')) { + $entity = t('Content'); + } + + $data['field_collection_item'][$pseudo_field_name]['relationship'] = array( + 'title' => t('Entity with the @field (@field_name)', array('@entity' => $entity, '@field' => $label, '@field_name' => $field['field_name'])), + 'help' => t('Relate each @entity using @field.', array('@entity' => $entity, '@field' => $label)), + 'handler' => 'views_handler_relationship_entity_reverse', + 'field_name' => $field['field_name'], + 'field table' => _field_sql_storage_tablename($field), + 'field field' => $field['field_name'] . '_value', + 'base' => $entity_info['base table'], + 'base field' => $entity_info['entity keys']['id'], + 'label' => t('!field_name', array('!field_name' => $field['field_name'])), + ); + } + + return $data; +} diff --git a/sites/all/modules/contrib/fields/field_collection/views/field_collection_handler_relationship.inc b/sites/all/modules/contrib/fields/field_collection/views/field_collection_handler_relationship.inc new file mode 100644 index 0000000..afbe121 --- /dev/null +++ b/sites/all/modules/contrib/fields/field_collection/views/field_collection_handler_relationship.inc @@ -0,0 +1,58 @@ + -1); + + return $options; + } + + /** + * Add a delta selector for multiple fields. + */ + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + + $field = field_info_field($this->definition['field_name']); + + // Only add the delta selector if the field is multiple. + if ($field['cardinality']) { + $max_delta = ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) ? 10 : $field['cardinality']; + + $options = array('-1' => t('All')); + for ($i = 0; $i < $max_delta; $i++) { + $options[$i] = $i + 1; + } + $form['delta'] = array( + '#type' => 'select', + '#options' => $options, + '#default_value' => $this->options['delta'], + '#title' => t('Delta'), + '#description' => t('The delta allows you to select which item in a multiple value field to key the relationship off of. Select "1" to use the first item, "2" for the second item, and so on. If you select "All", each item in the field will create a new row, which may appear to cause duplicates.'), + ); + } + } + + function ensure_my_table() { + $field = field_info_field($this->definition['field_name']); + + if (!isset($this->table_alias)) { + $join = $this->get_join(); + if ($this->options['delta'] != -1 && $field['cardinality']) { + $join->extra[] = array( + 'field' => 'delta', + 'value' => $this->options['delta'], + 'numeric' => TRUE, + ); + } + $this->table_alias = $this->query->add_table($this->table, $this->relationship, $join); + } + return $this->table_alias; + } +} diff --git a/sites/all/modules/contrib/fields/video_embed_field/LICENSE.txt b/sites/all/modules/contrib/fields/video_embed_field/LICENSE.txt new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/LICENSE.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/sites/all/modules/contrib/fields/video_embed_field/plugins/export_ui/video_embed_field_export_ui.inc b/sites/all/modules/contrib/fields/video_embed_field/plugins/export_ui/video_embed_field_export_ui.inc new file mode 100755 index 0000000..1ac41d6 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/plugins/export_ui/video_embed_field_export_ui.inc @@ -0,0 +1,26 @@ + 'vef_video_styles', // As defined in hook_schema(). + 'access' => 'administer video styles', // Define a permission users must have to access these pages. + // Define the menu item. + 'menu' => array( + 'menu prefix' => 'admin/config/media', + 'menu item' => 'vef_video_styles', + 'menu title' => 'Configure Video Embed Styles', + 'menu description' => 'Administer Video Embed Field\'s video styles.', + ), + // Define user interface texts. + 'title singular' => t('video style'), + 'title plural' => t('video styles'), + 'title singular proper' => t('Video Styles'), + 'title plural proper' => t('Video Styles'), + // Define the names of the functions that provide the add/edit forms. + 'form' => array( + 'settings' => 'video_embed_field_video_style_form', + // 'submit' and 'validate' are also valid callbacks. + ), +); diff --git a/sites/all/modules/contrib/fields/video_embed_field/video-embed-field-embed-code.tpl.php b/sites/all/modules/contrib/fields/video_embed_field/video-embed-field-embed-code.tpl.php new file mode 100644 index 0000000..b049be9 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video-embed-field-embed-code.tpl.php @@ -0,0 +1,22 @@ + + +
+
+ +
+
\ No newline at end of file diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_facebook/video_embed_facebook.info b/sites/all/modules/contrib/fields/video_embed_field/video_embed_facebook/video_embed_facebook.info new file mode 100644 index 0000000..30141ba --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_facebook/video_embed_facebook.info @@ -0,0 +1,13 @@ +name = "Video Embed Facebook" +description = "Provides Facebook handler for Video Embed Fields. This module also serves as an example of how to add handlers to video embed field." +core = 7.x +package = Media +configure = admin/config/media/vef_video_styles + +dependencies[] = "video_embed_field" +; Information added by drupal.org packaging script on 2012-10-17 +version = "7.x-2.0-beta5+11-dev" +core = "7.x" +project = "video_embed_field" +datestamp = "1350438417" + diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_facebook/video_embed_facebook.module b/sites/all/modules/contrib/fields/video_embed_field/video_embed_facebook/video_embed_facebook.module new file mode 100644 index 0000000..d4c1db8 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_facebook/video_embed_facebook.module @@ -0,0 +1,149 @@ + 'Facebook Video', //The title is the name to show to users + //function is a function to take the url and return the embed code + 'function' => 'video_embed_facebook_handle_video', + //thumbnail_function is optional and takes the url and returns the thumbnail url + 'thumbnail_function' => 'video_embed_facebook_handle_thumbnail', + //data_function is optional and returns an array of extra data for the given video url + //because facebook requires oath to get this data, we'll leave it out for now + //'data_function' => 'video_embed_facebook_handle_data', + //form is the configure form to embed into video_embed styles - this is where all settings should go + 'form' => 'video_embed_facebook_form', + //domains is how video embed determines which handler to use, its an array of domains to match + //urls against. Don't include the scheme (like http://) or www. + 'domains' => array( + 'facebook.com', + ), + //defaults are the defaults to provide to your form (as defined in your form callback) + 'defaults' => array( + 'width' => 640, + 'height' => 360, + 'allowfullscreen' => TRUE, + ), + ); + + return $handlers; +} + +/** + * Our configuration form (the callback for the form key in info) + * Provide a form to configure out video settings + * @param $defaults - default/current values for your provider, the currently saved settings + * with empty values filled with the defaults provided in info hook + * @return a form as defined by forms api + */ +function video_embed_facebook_form($defaults) { + $form = array(); + //form element for the width of the player - note we're using the default from defaults + $form['height'] = array( + '#type' => 'textfield', + '#title' => t('Player Width'), + '#description' => t('The width of the player.'), + '#default_value' => $defaults['height'], + ); + //element for width + $form['width'] = array( + '#type' => 'textfield', + '#title' => t('Player Width'), + '#description' => t('The width of the player.'), + '#default_value' => $defaults['width'], + ); + //allow configuration of fullscreen + $form['allowfullscreen'] = array( + '#type' => 'checkbox', + '#title' => t('Allow Fullscreen'), + '#desecription' => t('This will allow the video to be fullscreened.'), + '#default_value' => $defaults['allowfullscreen'], + ); + + return $form; +} + + +/** + * This is the video handler (the 'function' key from handler_info) + * @param $url - the full video url + * @param $settings - an associative array of this handlers settings, from the settings form + * @return - the embed code for the video + */ +function video_embed_facebook_handle_video($url, $settings) { + $id = _video_embed_facebook_get_video_id($url); + + if ($id) { + //our embed code + $embed = ' + + + + + + + '; + //use format_string to replace our placeholders with the values from the settings + $embed = format_string($embed, array( + '!id' => $id, + '!fullscreen' => $settings['allowfullscreen'] ? 'true' : 'false', + '!width' => $settings['width'], + '!height' => $settings['height'], + )); + //we want to return a render array + $video = array( + '#markup' => $embed, + ); + return $video; + } + // just return an empty string if there is no id, so we don't have broken embeds showing up + return ''; +} + +/** + * Retreive the thumbnail for the facebook video - note that based on a users permissions, this may be + * the facebook unknown thumbnail: https://s-static.ak.facebook.com/rsrc.php/v1/y0/r/XsEg9L6Ie5_.jpg + * @param $url - the url of the video as entered by the user + * @return an array with the keys: + * 'id' => an id for the video which is unique to your provider, used for naming the cached thumbnail file + * 'url' => the url to retrieve the thumbnail from + */ +function video_embed_facebook_handle_thumbnail($url) { + $id = _video_embed_facebook_get_video_id($url); + // We're using a part of the graphs api to return the thumbnail. This only works for some videos + return array( + 'id' => $id, //generally the id that the provider uses for the video + 'url' => 'https://graph.facebook.com/' . $id . '/picture', //the url of the thumbnail + ); +} + +/** + * Helper function to take a facebook video url and return its id + * @param $url - the full facebook video url + * @return the id for the video + */ +function _video_embed_facebook_get_video_id($url) { + //parse_url is an easy way to break a url into its components + $matches = array(); + preg_match('/(.*)?v=([^&#]*)/', $url, $matches); + //if the v get parameter is set, return it + if ($matches && !empty($matches[2])) { + return $matches[2]; + } + //otherwise return false. + return FALSE; +} diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.admin.inc b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.admin.inc new file mode 100644 index 0000000..3a3f029 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.admin.inc @@ -0,0 +1,31 @@ + 'vertical_tabs', + '#title' => t('Playback settings'), + '#tree' => TRUE, + ) + $parser_form; //add in our extra settings + + return $form; +} diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.api.php b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.api.php new file mode 100755 index 0000000..0f40367 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.api.php @@ -0,0 +1,100 @@ + 'UStream', + 'function' => 'your_module_handle_ustream', + 'thumbnail_function' => 'your_module_handle_ustream_thumbnail', + 'data_function' => 'your_module_handler_ustream_data', + 'form' => 'your_module_handler_ustream_form', + 'domains' => array( + 'ustream.com', + ), + 'defaults' => array( + 'width' => 640, + 'height' => 360, + ), + ); + + return $handlers; +} + +// Example callbacks for a provider (in this case for ustream) - obviously, these functions are only for example +// purposes + +/** + * Generate the embed code for a video + * @param $url - the video url as entered by the user + * @param $settings - the settings for this provider as defined in the form function, + * defaulting to your provider's defaults + * @return the embed code as a renderable array + */ +function your_module_handle_ustream($url, $settings) { + return array( + //this should be the full embed code for your provider, including each of the settings + '#markup' => '', + ); +} + +/** + * Retrieve information about the thumbnail for a given url + * @param $url - the url of the video as entered by the user + * @return an array with the keys: + * 'id' => an id for the video which is unique to your provider, used for naming the cached thumbnail file + * 'url' => the url to retrieve the thumbnail from + */ +function your_module_handle_ustream_thumbnail($url) { + return array( + 'id' => '12332243242', //generally the id that the provider uses for the video + 'url' => 'http://something/thumbnail/somthing.jpg', //the url of the thumbnail + ); +} + +/** + * A forms api callback, returns the settings form for the provider + * @param $defaults - default/current values for your provider, the currently saved settings + * with empty values filled with the defaults provided in info hook + * @return a form as defined by forms api + * + * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7 + */ +function your_module_handler_ustream_form($defaults) { + $form = array(); + + $form['width'] = array( + '#type' => 'textfield', + '#title' => t('Player Width'), + '#description' => t('The width of the player in pixels'), + '#default_value' => $defaults['width'], + ); + + return $form; +} + +/** + * Return an array of extra data to be stored with the video, this data will be available for theming + * @return an array + */ +function your_module_handler_ustream_data($url) { + return array(); +} diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.feeds.inc b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.feeds.inc new file mode 100644 index 0000000..5196383 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.feeds.inc @@ -0,0 +1,43 @@ + $instance) { + $info = field_info_field($name); + if ($info['type'] == 'video_embed_field') { + $targets[$name] = array( + 'name' => $instance['label'], + 'callback' => 'video_embed_field_set_target', + 'description' => t('The @label field of the node.', array('@label' => $instance['label'])), + ); + } + } +} + +/** + * Callback for mapping. Here is where the actual mapping happens. + * + * When the callback is invoked, $target contains the name of the field the + * user has decided to map to and $value contains the value of the feed item + * element the user has picked as a source. + */ +function video_embed_field_set_target($source, $entity, $target, $value) { + if (empty($value)) { + return; + } + + $field = isset($entity->$target) ? $entity->$target : array(); + if (!is_array($value) && !is_object($value)) { + $field['und'][0]['video_url'] = $value; + } + $entity->{$target} = $field; + +} diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.field.inc b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.field.inc new file mode 100755 index 0000000..3f2b1cc --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.field.inc @@ -0,0 +1,443 @@ + array( + 'label' => 'Video Embed', + 'description' => 'Embed videos from youtube or vimeo', + 'settings' => array(), + 'instance_settings' => array( + 'description_field' => 0, + 'description_length' => 128, + ), + 'default_widget' => 'video_embed_field_video', + 'default_formatter' => 'video_embed_field', + ), + ); +} + +/** + * Implements hook_field_instance_settings_form(). + */ +function video_embed_field_field_instance_settings_form($field, $instance) { + $settings = $instance['settings']; + + $form['description_field'] = array( + '#type' => 'checkbox', + '#title' => t('Enable Description field'), + '#default_value' => isset($settings['description_field']) ? $settings['description_field'] : '', + '#description' => t('The description field allows users to enter a description about the video.'), + '#parents' => array('instance', 'settings', 'description_field'), + '#weight' => 11, + ); + + $form['description_length'] = array( + '#type' => 'textfield', + '#title' => t('Max description length'), + '#default_value' => isset($settings['description_length']) ? $settings['description_length'] : 128, + '#parents' => array('instance', 'settings', 'description_length'), + '#weight' => 12, + '#size' => 5, + '#maxlength' => 5, + ); + + return $form; +} + +/** + * Implements hook_field_widget_info(). + */ +function video_embed_field_field_widget_info() { + return array( + 'video_embed_field_video' => array( + 'label' => 'Video', + 'description' => 'Provides a video embed field', + 'field types' => array('video_embed_field'), + 'settings' => array(), + 'behaviors' => array( + 'multiple values' => FIELD_BEHAVIOR_DEFAULT, + 'default value' => FIELD_BEHAVIOR_DEFAULT, + ), + ), + ); +} + +/** + * Implements hook_field_widget_form(). + */ +function video_embed_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + // Don't need to check the type right now because we're only defining one + $element += array( + '#type' => 'fieldset', + ); + + $element['video_url'] = array( + '#type' => 'textfield', + '#title' => t('Video URL'), + '#attributes' => array('class' => array('video_embed_url')), + '#attached' => array( + 'css' => array( + drupal_get_path('module', 'video_embed_field') . '/video_embed_field.form.css', + ), + ), + '#default_value' => isset($items[$delta]['video_url']) ? $items[$delta]['video_url'] : '', + '#required' => $element['#required'], + ); + + // Add the description field if enabled. + if (!empty($instance['settings']['description_field'])) { + $element['description'] = array( + '#type' => 'textfield', + '#title' => t('Description'), + '#default_value' => isset($items[$delta]['description']) ? $items[$delta]['description'] : '', + '#description' => t('The description which may be used as a label.'), + '#maxlength' => $instance['settings']['description_length'], + ); + } + + return $element; +} + +/** + * Validates video URL. + */ +function video_embed_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { + foreach ($items as $delta => $item) { + if (!empty($item['video_url'])) { + $item['video_url'] = trim($item['video_url']); + if (stristr($item['video_url'], '.') && !stristr($item['video_url'], 'http://') && !stristr($item['video_url'], 'https://')) { + $item['video_url'] = 'http://' . $item['video_url']; + } + + $parts = parse_url($item['video_url']); + if (!$parts || !isset($parts['host'])) { + $errors[$field['field_name']][$langcode][$delta][] = array( + 'error' => t('Invalid Url'), + 'message' => t('Video: Invalid Video URL.', array('%name' => $instance['label'])), + ); + } + else { + $host = $parts['host']; + if (stripos($host, 'www.') > -1) { + $host = substr($host, 4); + } + $domains = _video_embed_field_get_provider_domains(); + if (!array_key_exists($host, $domains)) { + $errors[$field['field_name']][$langcode][$delta][] = array( + 'error' => t('Unsupported Video Provider'), + 'message' => t('%name: This video provider is not currently supported.', array('%name' => $instance['label'])), + ); + } + } + } + } +} + +/** + * Implementation of hook_field_presave(). + * + * Download and save the thumbnail if it hasn't already been stored. + * Get video data. + */ +function video_embed_field_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { + foreach ($items as $delta => $item) { + //Trim whitespace from the video URL. + $items[$delta]['video_url'] = trim($item['video_url']); + + // Try to load thumbnail URL + $info = video_embed_field_thumbnail_url($item['video_url']); + if (isset($info['url']) && $info['url']) { + $thumb_url = $info['url']; + $local_path = 'public://video_embed_field_thumbnails/' . $info['handler'] . '/' . $info['id'] . '.jpg'; + + $dirname = drupal_dirname($local_path); + file_prepare_directory($dirname, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); + + $response = drupal_http_request($thumb_url); + if (!isset($response->error)) { + file_save_data($response->data, $local_path, TRUE); + } + else { + @copy($thumb_url, $local_path); + } + + + $items[$delta]['thumbnail_path'] = $local_path; + } //couldn't get the thumbnail for whatever reason + else { + $items[$delta]['thumbnail_path'] = ''; + } + + // Try to load video data + $data = video_embed_field_get_video_data($item['video_url']); + if (is_array($data) && !empty($data)) { + $items[$delta]['video_data'] = serialize($data); + } + else { + $items[$delta]['video_data'] = NULL; + } + + } +} + +/** + * Implements hook_field_is_empty(). + */ +function video_embed_field_field_is_empty($item, $field) { + return empty($item) || empty($item['video_url']) || $item['video_url'] === ''; +} + +/** + * Implements hook_field_formatter_info(). + */ +function video_embed_field_field_formatter_info() { + $info = array( + 'video_embed_field' => array( + 'label' => t('Video Player'), + 'field types' => array('video_embed_field'), + 'settings' => array( + 'video_style' => 'normal', + 'description' => 1, + 'description_position' => 'bottom' + ), + ), + 'video_embed_field_thumbnail' => array( + 'label' => t('Thumbnail Preview'), + 'field types' => array('video_embed_field'), + 'settings' => array( + 'image_style' => 'none', + 'description' => 1, + 'description_position' => 'bottom', + 'image_link' => 'none', + ), + ), + ); + if ( module_exists('colorbox') ) { + $info['video_embed_field_thumbnail_colorbox'] = array( + 'label' => t('Thumbnail Preview w/Colorbox'), + 'field types' => array('video_embed_field'), + 'settings' => array( + 'video_style' => 'normal', + 'image_style' => 'none', + 'description' => 1, + 'description_position' => 'bottom', + ), + ); + } + return $info; +} + +/** + * Implements hook_field_formatter_settings_form(). + */ +function video_embed_field_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + $element = array(); + + if ($display['type'] == 'video_embed_field' || $display['type'] == 'video_embed_field_thumbnail_colorbox') { + $video_styles = video_embed_field_video_style_options(FALSE); + $element['video_style'] = array( + '#title' => t('Video style'), + '#type' => 'select', + '#default_value' => $settings['video_style'], + '#options' => $video_styles, + ); + } + if ($display['type'] == 'video_embed_field_thumbnail' || $display['type'] == 'video_embed_field_thumbnail_colorbox') { + $element['image_style'] = array( + '#title' => t('Image style'), + '#type' => 'select', + '#options' => image_style_options(FALSE), + '#default_value' => $settings['image_style'], + '#empty_option' => t('None (original image)'), + ); + } + + if ($display['type'] == 'video_embed_field_thumbnail') { + $link_types = array( + 'node' => t('Node'), + 'source' => t('Video Source'), + ); + $element['image_link'] = array( + '#title' => t('Link thumbnail to'), + '#type' => 'select', + '#default_value' => $settings['image_link'], + '#empty_option' => t('Nothing'), + '#options' => $link_types, + ); + } + + if ($instance['settings']['description_field']) { + $element['description'] = array( + '#title' => t('Show description'), + '#type' => 'checkbox', + '#default_value' => $settings['description'], + ); + + $element['description_position'] = array( + '#title' => t('Description Position'), + '#type' => 'select', + '#options' => array( + 'top' => t('Top'), + 'bottom' => t('Bottom') + ), + '#default_value' => $settings['description_position'], + ); + } + return $element; +} +/** + * Implements hook_field_formatter_settings_summary(). + */ +function video_embed_field_field_formatter_settings_summary($field, $instance, $view_mode) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + + $summary = array(); + if ($display['type'] == 'video_embed_field' || $display['type'] == 'video_embed_field_thumbnail_colorbox') { + $video_styles = video_embed_field_video_style_options(FALSE); + // Styles could be lost because of enabled/disabled modules that defines + // their styles in code. + if (isset($video_styles[$settings['video_style']])) { + $summary[] = t('Video style: @style', array('@style' => $video_styles[$settings['video_style']])); + } + } + if ($display['type'] == 'video_embed_field_thumbnail' || $display['type'] == 'video_embed_field_thumbnail_colorbox') { + $image_styles = image_style_options(FALSE); + if (isset($image_styles[$settings['image_style']])) { + $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']])); + } //No Image style (original image) + else { + $summary[] = t('Original Image.'); + } + if (isset($settings['image_link'])) { + $summary[] = t('Image link: ' . $settings['image_link']); + } + else { + $summary[] = t('Image link: none'); + } + } + + if ($settings['description'] && $instance['settings']['description_field']) { + $summary[] = t('Show description'); + } + elseif ($instance['settings']['description_field']) { + $summary[] = t('Hide description'); + } + + return implode('
', $summary); +} + +/** + * Implements hook_field_formatter_view(). + */ +function video_embed_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $element = array(); + $settings = $display['settings']; + + foreach ($items as $delta => $item) { + //create the render array for the description + if (isset($item['description']) && $item['description'] && $settings['description'] && $instance['settings']['description_field']) { + $description = array( + '#prefix' => '
', + '#markup' => $item['description'], + '#suffix' => '
', + ); + } + else { + $description = array(); + } + + //Render the field + if ($display['type'] == 'video_embed_field') { + $element[$delta] = array( + array( + '#theme' => 'video_embed_field_embed_code', + '#url' => $item['video_url'], + '#style' => $settings['video_style'], + '#video_data' => !empty($item['video_data']) ? unserialize($item['video_data']) : array(), + ), + ); + } + elseif ($display['type'] == 'video_embed_field_thumbnail') { + if (isset($item['thumbnail_path'])) { + if (empty($settings['image_style']) || $settings['image_style'] == 'none') { + $element[$delta] = array( + array( + '#theme' => 'image', + '#path' => $item['thumbnail_path'], + ), + ); + } + else { + $element[$delta] = array( + array( + '#theme' => 'image_style', + '#path' => $item['thumbnail_path'], + '#style_name' => $settings['image_style'], + ), + ); + } + if ($settings['image_link'] == 'source') { + $link = explode('|||', l('|||', $item['video_url'])); + $element[$delta]['#prefix'] = $link[0]; + $element[$delta]['#suffix'] = $link[1]; + } + elseif ($settings['image_link'] == 'node') { + $nid = $entity->nid; + $link = explode('|||', l('|||', 'node/' . $nid)); + $element[$delta]['#prefix'] = $link[0]; + $element[$delta]['#suffix'] = $link[1]; + } + } //incase no thumbnail was downloaded / provider doesn't support thumbnails + else { + $element[$delta] = array( + ); + } + } + elseif ($display['type'] == 'video_embed_field_thumbnail_colorbox') { + if (isset($item['thumbnail_path'])) { + $element[$delta] = array( + array( + '#theme' => 'video_embed_field_colorbox_code', + '#image_url' => $item['thumbnail_path'], + '#image_style' => $display['settings']['image_style'], + '#video_url' => $item['video_url'], + '#video_style' => $display['settings']['video_style'], + '#video_data' => unserialize($item['video_data']), + ), + ); + } + //incase no thumbnail was downloaded / provider doesn't support thumbnails + else { + $element[$delta] = array( + ); + } + } + //Get the HTML instead of the array, because we append it to the suffix. + //This way, the thumbnail link wrapper doesn't make the description a link as well. + $description_html = drupal_render($description); + + $pos = isset($settings['description_position']) ? $settings['description_position'] : 'bottom'; + if ($pos == 'top') { + $element[$delta]['#prefix'] = isset($element[$delta]['#prefix']) ? $element[$delta]['#prefix'] : ''; + $element[$delta]['#prefix'] = $description_html . $element[$delta]['#prefix']; + } + else { + $element[$delta]['#suffix'] = isset($element[$delta]['#suffix']) ? $element[$delta]['#suffix'] : ''; + $element[$delta]['#suffix'] .= $description_html; + } + } + + + return $element; +} diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.form.css b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.form.css new file mode 100644 index 0000000..e3db585 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.form.css @@ -0,0 +1,4 @@ +/* Style to make sure the fields are the right way */ +table input.fluid.video_embed_url { + width:100%; +} \ No newline at end of file diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.handlers.inc b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.handlers.inc new file mode 100755 index 0000000..f0824bd --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.handlers.inc @@ -0,0 +1,519 @@ + 'Youtube', + 'function' => 'video_embed_field_handle_youtube', + 'thumbnail_function' => 'video_embed_field_handle_youtube_thumbnail', + 'data_function' => 'video_embed_field_handle_youtube_data', + 'form' => 'video_embed_field_handler_youtube_form', + 'domains' => array( + 'youtube.com', + 'youtu.be', + ), + 'defaults' => array( + 'width' => '640px', + 'height' => '360px', + 'autoplay' => 0, + 'hd' => 1, + 'rel' => 0, + 'autohide' => 2, + 'showinfo' => 1, + 'modestbranding' => 0, + 'theme' => 'dark', + 'iv_load_policy' => 1, + ), + ); + + $handlers['vimeo'] = array( + 'title' => 'Vimeo', + 'function' => 'video_embed_field_handle_vimeo', + 'thumbnail_function' => 'video_embed_field_handle_vimeo_thumbnail', + 'data_function' => 'video_embed_field_handle_vimeo_data', + 'form' => 'video_embed_field_handler_vimeo_form', + 'domains' => array( + 'vimeo.com', + ), + 'defaults' => array( + 'width' => '640px', + 'height' => '360px', + 'color' => '00adef', + 'portrait' => 1, + 'title' => 1, + 'byline' => 1, + 'autoplay' => 0, + 'loop' => 0, + ), + ); + + return $handlers; +} + +/** + * Helper function to get the youtube video's id + * Returns false if it doesn't parse for wahtever reason + */ +function _video_embed_field_get_youtube_id($url) { + // Find the ID of the video they want to play from the url + if (stristr($url, 'http://')) { + $url = substr($url, 7); + } + elseif (stristr($url, 'https://')) { + $url = substr($url, 8); + } + + if (stristr($url, 'playlist')) { + //Playlists need the appended ampersand to take the options properly. + $url = $url . '&'; + $pos = strripos($url, '?list='); + if ($pos !== FALSE) { + $pos2 = stripos($url, '&'); + $pos2++; + } + else { + return FALSE; + } + } + //Alternate playlist link + elseif (stristr($url, 'view_play_list')) { + $url = $url . '&'; + //All playlist ID's are prepended with PL. view_play_list links allow you to not have that, though. + if (!stristr($url, '?p=PL')) { + $url = substr_replace($url, 'PL', strpos($url, '?p=') + 3, 0); + } + //Replace the links format with the embed format + $url = str_ireplace('play_list?p=', 'videoseries?list=', $url); + $pos = strripos($url, 'videoseries?list='); + if ($pos !== FALSE) { + $pos2 = stripos($url, '&'); + $pos2++; + } + else { + return FALSE; + } + } + else { + $pos = strripos($url, 'v='); + if ($pos !== FALSE) { + $pos += 2; + $pos2 = stripos($url, '&', $pos); + $pos_hash = stripos($url, '#', $pos); + + $pos2 = _video_embed_get_min($pos2, $pos_hash); + } + else { + $pos = strripos($url, '/'); + if ($pos !== FALSE) { + $pos++; + $pos2 = stripos($url, '?', $pos); + $pos_hash = stripos($url, '#', $pos); + + $pos2 = _video_embed_get_min($pos2, $pos_hash); + } + } + } + if ($pos === FALSE) { + return FALSE; + } + else { + if ($pos2 > 0) { + $id = substr($url, $pos, $pos2 - $pos); + } + else { + $id = substr($url, $pos); + } + } + return $id; +} + +/** + * Handler for Youtube videos. + */ +function video_embed_field_handle_youtube($url, $settings) { + $output = array(); + + //Grab the minutes and seconds, and just convert it down to seconds + preg_match('/#t=((?P\d+)m)?((?P\d+)s)?/', $url, $matches); + + //Give it some default data in case there is no #t=... + $matches += array( + "min" => 0, + "sec" => 0, + ); + $time = ($matches["min"] * 60) + $matches["sec"]; + $settings['start'] = $time; + + $id = _video_embed_field_get_youtube_id($url); + if (!$id) { + // We can't decode the URL - just return the URL as a link + $output['#markup'] = l($url, $url); + return $output; + } + // Construct the embed code + $settings['wmode'] = 'opaque'; + $settings_str = _video_embed_code_get_settings_str($settings); + + $output['#markup'] = ''; + + return $output; +} + +/** + * Get the thumbnail url for youtube videos + */ +function video_embed_field_handle_youtube_thumbnail($video_url) { + $info = array(); + + $id = _video_embed_field_get_youtube_id($video_url); + + //Playlist + if (stristr($id, '?list=')) { + //Strip out all but the ID, including the PL behind the ID. + $start = strpos($id, '?list=PL') + 8; + $length = strpos($id, '&') - $start; + $id = substr($id, $start, $length); + $info['id'] = $id; + //Playlist info is stored in XML. The thumbnail is in there. + $xml = drupal_http_request('http://gdata.youtube.com/feeds/api/playlists/' . $id); + if (!isset($xml->error)) { + $xml = new SimpleXMLElement($xml->data); + $media = $xml->children('http://search.yahoo.com/mrss/'); + if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) { + $attrs = $media->group->thumbnail[0]->attributes(); + $info['url'] = (string) $attrs['url']; + } + } + } + //Regular video + elseif ($id) { + $info['id'] = $id; + $info['url'] = 'http://img.youtube.com/vi/' . $id . '/0.jpg'; + } + return $info; +} + +/** + * Get video data for a YouTube video URL + * + * @param string $url + * A YouTube video URL to get data for + * + * @return array|false $data + * An array of video data, or FALSE if unable to fetch data + */ +function video_embed_field_handle_youtube_data($url) { + $data = array(); + + // Get YouTube video ID from URL + $id = _video_embed_field_get_youtube_id($url); + + if ($id) { + $response = drupal_http_request('http://gdata.youtube.com/feeds/api/videos/' . $id . '?v=2&alt=json'); + if (!isset($response->error)) { + $data = json_decode($response->data); + $data = isset($data->entry) ? (array) $data->entry : (array) $data->feed; + return _video_embed_field_clean_up_youtube_data($data); + } + } + + return FALSE; +} + +/** + * Flatten out some unnecessary nesting in the youtube data + */ +function _video_embed_field_clean_up_youtube_data($data) { + //make things a bit nicer for people trying to use the data + foreach ($data as $key => $value) { + if (is_object($value)) { + $temp = (array) $value; + if (isset($temp['$t'])) { + $data[$key] = $temp['$t']; + } + else { + $data[$key] = _video_embed_field_clean_up_youtube_data($temp); + } + } + elseif (is_array($value)) { + $data[$key] = _video_embed_field_clean_up_youtube_data($value); + } + + if ($key === 'category') { + $terms = array(); + foreach ($data[$key] as $value) { + if (isset($value['scheme']) && $value['scheme'] == 'http://schemas.google.com/g/2005#kind') { + continue; + } + + if (isset($value['term'])) { + $terms[] = $value['term']; + } + } + $data['terms'] = $terms; + } + } + return $data; +} + +/** + * Defines the form elements for the Youtube configuration form. + */ +function video_embed_field_handler_youtube_form($defaults) { + $form = array(); + + $form['width'] = array( + '#type' => 'textfield', + '#size' => '5', + '#title' => t('Player Width'), + '#description' => t('The width of the youtube player.'), + '#default_value' => $defaults['width'], + ); + $form['height'] = array( + '#type' => 'textfield', + '#size' => '5', + '#title' => t('Player Height'), + '#description' => t('The height of the youtube player.'), + '#default_value' => $defaults['height'], + ); + $form['theme'] = array( + '#type' => 'select', + '#options' => array( + 'dark' => t('Dark'), + 'light' => t('Light'), + ), + '#title' => t('Player theme'), + '#default_value' => $defaults['theme'], + ); + $form['autoplay'] = array( + '#type' => 'checkbox', + '#title' => t('Autoplay'), + '#description' => t('Play the video immediately.'), + '#default_value' => $defaults['autoplay'], + ); + $form['hd'] = array( + '#type' => 'checkbox', + '#title' => t('Use HD'), + '#description' => t('Attempt to play the video in HD if available.'), + '#default_value' => $defaults['hd'], + ); + $form['rel'] = array( + '#type' => 'checkbox', + '#title' => t('Show related videos'), + '#description' => t('Show related videos after the video is finished playing.'), + '#default_value' => $defaults['rel'], + ); + $form['showinfo'] = array( + '#type' => 'checkbox', + '#title' => t('Show info'), + '#description' => t('Display information like the video title and rating before the video starts playing.'), + '#default_value' => $defaults['showinfo'], + ); + $form['modestbranding'] = array( + '#type' => 'checkbox', + '#title' => t('Hide Youtube logo'), + '#description' => t('Hide the Youtube logo button on the player'), + '#default_value' => $defaults['modestbranding'], + ); + $form['iv_load_policy'] = array( + '#type' => 'radios', + '#options' => array( + 1 => t('Show video annotations.'), + 3 => t('Hide video annotations.'), + ), + '#title' => t('Display annotations'), + '#description' => t('Controls the display of annotations over the video content. Only works when using the flash player.'), + '#default_value' => $defaults['iv_load_policy'], + ); + $form['autohide'] = array( + '#type' => 'radios', + '#options' => array( + 0 => t('The video progress bar and player controls will be visible throughout the video.'), + 1 => t('Automatically slide the video progress bar and the player controls out of view a couple of seconds after the video starts playing. They will only reappear if the user moves her mouse over the video player or presses a keyboard key.'), + 2 => t('The video progress bar will fade out but the player controls (play button, volume control, etc.) remain visible.'), + ), + '#title' => t('Autohide progress bar and the player controls'), + '#description' => t('Controls the autohide behavior of the youtube player controls.'), + '#default_value' => $defaults['autohide'], + ); + + return $form; +} + +/** + * Helper function to get the Vimeo video's ID + * + * @param string $url + * A Vimeo video URL to get the ID of + * + * @return integer|false $id + * The video ID, or FALSE if unable to get the video ID + */ +function _video_embed_field_get_vimeo_id($url) { + $pos = strripos($url, '/'); + if ($pos != FALSE) { + $pos += 1; + return (int) substr($url, $pos); + } + return FALSE; +} + +/** + * Handler for Vimeo videos. + */ +function video_embed_field_handle_vimeo($url, $settings) { + // Get ID of video from URL + $id = _video_embed_field_get_vimeo_id($url); + if (!$id) { + return array( + '#markup' => l($url, $url), + ); + } + + // Construct the embed code + $settings['portrait'] = 0; + $settings_str = _video_embed_code_get_settings_str($settings); + + return array( + '#markup' => '', + ); +} + +/** + * Get the thumbnail url for youtube videos + */ +function video_embed_field_handle_vimeo_thumbnail($url) { + // Get ID of video from URL + $id = _video_embed_field_get_vimeo_id($url); + $info = array( + 'id' => $id, + ); + $response = drupal_http_request('http://vimeo.com/api/v2/video/' . $id . '.php'); + if (!isset($response->error)) { + $response = unserialize($response->data); + $video = current($response); + + $image_url = $video['thumbnail_large']; + $info['url'] = $image_url; + } + return $info; +} + +/** + * Get video data for a Vimeo video URL + * + * @param string $url + * A Vimeo video URL to get data for + * + * @return array|false $data + * An array of video data, or FALSE if unable to fetch data + */ +function video_embed_field_handle_vimeo_data($url) { + // Get ID of video from URL + $id = _video_embed_field_get_vimeo_id($url); + if ($id) { + $response = drupal_http_request('http://vimeo.com/api/v2/video/' . $id . '.php'); + if (!isset($response->error)) { + $response = unserialize($response->data); + return (array) current($response); + } + } + return FALSE; +} + +/** + * Defines the form elements for the Vimeo configuration form. + */ +function video_embed_field_handler_vimeo_form($defaults) { + $form = array(); + + $form['width'] = array( + '#type' => 'textfield', + '#size' => '5', + '#title' => t('Player Width'), + '#description' => t('The width of the vimeo player.'), + '#default_value' => $defaults['width'], + ); + $form['height'] = array( + '#type' => 'textfield', + '#size' => '5', + '#title' => t('Player Height'), + '#description' => t('The height of the vimeo player.'), + '#default_value' => $defaults['height'], + ); + $form['color'] = array( + '#type' => 'select', + '#options' => array( + '00adef' => t('Blue'), + 'ff9933' => t('Orange'), + 'c9ff23' => t('Lime'), + 'ff0179' => t('Fuschia'), + 'ffffff' => t('White'), + ), + '#title' => t('Player Color'), + '#description' => t('The color to use on the vimeo player.'), + '#default_value' => $defaults['color'], + ); + $form['portrait'] = array( + '#type' => 'checkbox', + '#title' => t('Overlay Author Thumbnail'), + '#description' => t('Overlay the author\'s thumbnail before the video is played.'), + '#default_value' => $defaults['portrait'], + ); + $form['title'] = array( + '#type' => 'checkbox', + '#title' => t('Overlay Video\'s Title'), + '#description' => t('Overlay the video\'s title before the video is played.'), + '#default_value' => $defaults['title'], + ); + $form['byline'] = array( + '#type' => 'checkbox', + '#title' => t('Overlay Video\'s Byline'), + '#description' => t('Overlay the video\'s description before the video is played.'), + '#default_value' => $defaults['byline'], + ); + $form['overridable'] = array( + '#prefix' => '

' . t('Note') . ': ', + '#markup' => t('Color, portrait, title and byline can be restricted by Vimeo Plus videos. + Such videos will ignore these settings.'), + '#suffix' => '

', + ); + $form['autoplay'] = array( + '#type' => 'checkbox', + '#title' => t('Autoplay'), + '#description' => t('Play the video immediately.'), + '#default_value' => $defaults['autoplay'], + ); + $form['loop'] = array( + '#type' => 'checkbox', + '#title' => t('Loop'), + '#description' => t('Loop the video\'s playback'), + '#default_value' => $defaults['loop'], + ); + return $form; +} + +/** + * Calculate the min index for use in finding the id of a youtube video + */ +function _video_embed_get_min($pos1, $pos2) { + if (!$pos1) { + return $pos2; + } + elseif (!$pos2) { + return $pos1; + } + else { + return min($pos1, $pos2); + } +} diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.info b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.info new file mode 100644 index 0000000..da965b3 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.info @@ -0,0 +1,14 @@ +name = "Video Embed Field" +description = "Expose a field type for embedding videos from youtube or vimeo." +core = 7.x +package = Media +configure = admin/config/media/vef_video_styles + +dependencies[] = ctools +dependencies[] = image +; Information added by drupal.org packaging script on 2012-10-17 +version = "7.x-2.0-beta5+11-dev" +core = "7.x" +project = "video_embed_field" +datestamp = "1350438417" + diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.install b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.install new file mode 100755 index 0000000..2537ab4 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.install @@ -0,0 +1,243 @@ + array( + 'type' => 'varchar', + 'length' => 512, + 'default' => '', + ), + 'thumbnail_path' => array( + 'type' => 'varchar', + 'length' => 512, + 'default' => '', + ), + 'video_data' => array( + 'type' => 'blob', + 'not null' => FALSE, + 'size' => 'big', + 'serialize' => TRUE, + ), + 'embed_code' => array( + 'type' => 'varchar', + 'length' => 1024, + 'default' => '', + ), + 'description' => array( + 'type' => 'text', + 'not null' => FALSE, + ), + ); + $indexes = array(); + break; + } + + return array( + 'columns' => $columns, + 'indexes' => $indexes, + ); +} + +/** + * Implements hook_schema(). + */ +function video_embed_field_schema() { + $schema['vef_video_styles'] = array( + 'description' => 'Stores video embed styles.', + 'export' => array( + 'key' => 'name', + 'primary key' => 'vsid', + 'identifier' => 'video_embed_style', // Exports will be as $video_style + 'default hook' => 'default_video_embed_styles', // Function hook name. + 'api' => array( + 'owner' => 'video_embed_field', + 'api' => 'default_video_embed_styles', // Base name for api include files. + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + 'fields' => array( + 'vsid' => array( + 'description' => 'The primary identifier for a video style.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'no export' => TRUE, + ), + 'name' => array( + 'description' => 'The style name.', + 'type' => 'varchar', + 'length' => '255', + 'not null' => TRUE, + ), + 'data' => array( + 'description' => 'The configuration data for the style.', + 'type' => 'blob', + 'size' => 'big', + 'not null' => TRUE, + 'serialize' => TRUE, + ), + ), + 'primary key' => array('vsid'), + 'unique keys' => array( + 'name' => array('name'), + ), + ); + return $schema; +} + +/** + * Implements hook_uninstall(). + */ +function video_embed_field_uninstall() { + //do nothing right now - should eventually remove all the variables +} + +/** + * Update 7000 + * Add an optional description form + */ +function video_embed_field_update_7000() { + // Get the list of fields of type 'video_embed_field'. + $video_embed_fields = array(); + foreach (field_info_fields() as $field_name => $field_info) { + if ($field_info['type'] == 'video_embed_field') { + $video_embed_fields[$field_name] = field_read_field($field_name); + } + } + + foreach ($video_embed_fields as $field) { + if ($field['deleted']) { + $table = "field_deleted_data_{$field['id']}"; + $revision_table = "field_deleted_revision_{$field['id']}"; + } + else { + $table = "field_data_{$field['field_name']}"; + $revision_table = "field_revision_{$field['field_name']}"; + } + $column = $field['field_name'] . '_' . 'description'; + db_add_field($table, $column, array('type' => 'text', 'not null' => FALSE)); + db_add_field($revision_table, $column, array('type' => 'text', 'not null' => FALSE)); + } + + return t('Additional columns added.'); +} + +/** + * Update 7001 + * Add video style storage table + */ +function video_embed_field_update_7001() { + if (!db_table_exists('vef_video_styles')) { + $schema = video_embed_field_schema(); + db_create_table('vef_video_styles', $schema['vef_video_styles']); + } + + return t('Video styles storage table created.'); +} +/** + * Update 7002 + * Add field for storing the path to the video thumbnail + */ +function video_embed_field_update_7002() { + // Get the list of fields of type 'video_embed_field'. + $video_embed_fields = array(); + foreach (field_info_fields() as $field_name => $field_info) { + if ($field_info['type'] == 'video_embed_field') { + $video_embed_fields[$field_name] = field_read_field($field_name); + } + } + + foreach ($video_embed_fields as $field) { + if ($field['deleted']) { + $table = "field_deleted_data_{$field['id']}"; + $revision_table = "field_deleted_revision_{$field['id']}"; + } + else { + $table = "field_data_{$field['field_name']}"; + $revision_table = "field_revision_{$field['field_name']}"; + } + $column = $field['field_name'] . '_' . 'thumbnail_path'; + db_add_field($table, $column, array( + 'type' => 'varchar', + 'length' => 512, + 'default' => '', + )); + db_add_field($revision_table, $column, array( + 'type' => 'varchar', + 'length' => 512, + 'default' => '', + )); + } + + return t('Thumbnail column added.'); +} + +/** + * Enable inline colorbox support if colorbox is installed [NO LONGER NEEDED - This update hook does nothing] + */ +function video_embed_field_update_7003() { + //this is no longer needed + //variable_set('colorbox_inline', 1); +} + +/** + * Enable colorbox load support if colorbox is installed, we no longer need inline support + */ +function video_embed_field_update_7004() { + variable_set('colorbox_load', 1); +} + +/** + * Add data column to field database. + */ +function video_embed_field_update_7005() { + // Get the list of fields of type 'video_embed_field'. + $video_embed_fields = array(); + foreach (field_info_fields() as $field_name => $field_info) { + if ($field_info['type'] == 'video_embed_field') { + $video_embed_fields[$field_name] = field_read_field($field_name); + } + } + + foreach ($video_embed_fields as $field) { + if ($field['deleted']) { + $table = "field_deleted_data_{$field['id']}"; + $revision_table = "field_deleted_revision_{$field['id']}"; + } + else { + $table = "field_data_{$field['field_name']}"; + $revision_table = "field_revision_{$field['field_name']}"; + } + $column = $field['field_name'] . '_' . 'video_data'; + db_add_field($table, $column, array( + 'type' => 'blob', + 'not null' => FALSE, + 'size' => 'big', + 'serialize' => TRUE, + )); + db_add_field($revision_table, $column, array( + 'type' => 'blob', + 'not null' => FALSE, + 'size' => 'big', + 'serialize' => TRUE, + )); + } + + return t('Data column added. Please clear cache.'); +} diff --git a/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.module b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.module new file mode 100755 index 0000000..8f25a71 --- /dev/null +++ b/sites/all/modules/contrib/fields/video_embed_field/video_embed_field.module @@ -0,0 +1,570 @@ + 1); + } +} + +/** + * Implements hook_default_video_styles(). + */ +function video_embed_field_default_video_embed_styles() { + $styles = array(); + + $handlers = video_embed_get_handlers(); + //create the normal handler + $normal = new stdClass; + $normal->disabled = FALSE; /* Edit this to true to make a default video_embed_style disabled initially */ + $normal->api_version = 1; + $normal->name = 'normal'; + $normal->data = array(); + + $teaser = new stdClass; + $teaser->disabled = FALSE; /* Edit this to true to make a default video_embed_style disabled initially */ + $teaser->api_version = 1; + $teaser->name = 'teaser'; + $teaser->data = array(); + + //add in our settings for each of the handlers + foreach ($handlers as $name => $handler) { + $normal->data[$name] = $handler['defaults']; + $teaser->data[$name] = $handler['defaults']; + $teaser->data[$name]['width'] = 480; + $teaser->data[$name]['height'] = 270; + } + + return array($normal, $teaser); +} + +/** + * Implements hook_menu(). + */ +function video_embed_field_menu() { + $items = array(); + + $items['vef/load/%'] = array( + 'title' => 'Video Embed Field - Load Video', + 'page callback' => '_video_embed_field_show_video', + 'page arguments' => array(2), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + + return $items; +} + +/** + * Get an array of all styles and their settings. + * + * @return + * An array of styles keyed by the video style ID (vsid). + * @see video_embed_field_video_style_load() + */ +function video_embed_field_video_styles() { + $styles = &drupal_static(__FUNCTION__); + + // Grab from cache or build the array. + if (!isset($styles)) { + // load the style via ctools - which will handle all the caching for us - + // however, because it does a bit more logic, lets still statically cache this function + ctools_include('export'); + $styles = ctools_export_load_object('vef_video_styles'); + } + + return $styles; +} + +/** + * Load a style by style name or ID. May be used as a loader for menu items. + * + * Note that you may also use ctools_export_load_object with the key being vef_video_styles + * + * @param $name + * The name of the style. + * @param $isid + * Optional. The numeric id of a style if the name is not known. + * @return + * An video style array containing the following keys: + * - "vsid": The unique image style ID. + * - "name": The unique image style name. + * - "data": An array of video settings within this video style. + * If the video style name or ID is not valid, an empty array is returned. + */ +function video_embed_field_video_style_load($name = NULL, $vsid = NULL) { + $styles = video_embed_field_video_styles(); + + // If retrieving by name. + if (isset($name) && isset($styles[$name])) { + $style = $styles[$name]; + } // If retrieving by image style id. + elseif (!isset($name) && isset($vsid)) { + foreach ($styles as $name => $database_style) { + if (isset($database_style['vsid']) && $database_style['vsid'] == $vsid) { + $style = $database_style; + break; + } + } + } + + //if we found a style return it + if (isset($style)) { + return $style; + } + + // Otherwise the style was not found. + return FALSE; +} + +/** + * Implements hook_permission(). + */ +function video_embed_field_permission() { + return array( + 'administer video styles' => array( + 'title' => t('Administer video styles'), + 'description' => t('Create and modify styles for embedded videos.'), + ), + ); +} + +/** + * Implements hook_theme(). + */ +function video_embed_field_theme() { + return array( + // Theme functions in video_embed_field.admin.inc. + 'video_embed_field_video_style_list' => array( + 'variables' => array('styles' => NULL), + ), + 'video_embed_field_embed_code' => array( + 'template' => 'video-embed-field-embed-code', + 'variables' => array('url' => NULL, 'style' => 'normal', 'video_data' => array()), + ), + 'video_embed_field_colorbox_code' => array( + 'variables' => array('image_url' => NULL, 'image_style' => 'normal', 'video_url' => NULL, 'video_style' => NULL, 'video_data' => array()), + ), + ); +} + +/** + * Creates a hook that other modules can implement to get handlers - hook_video_embed_handler_info + * Can be used to add more handlers if needed - from other modules and such + * @see video_embed_field.api.php for more information + */ +function video_embed_get_handlers() { + $handlers = cache_get('video_embed_field_handlers'); + + if ($handlers === FALSE) { + $handlers = module_invoke_all('video_embed_handler_info'); + drupal_alter('video_embed_field_handlers', $handlers); + cache_set('video_embed_field_handlers', $handlers); + } + else { + $handlers = $handlers->data; + } + + return $handlers; +} + +function video_embed_get_handler($url) { + // Process video URL + if (!stristr($url, 'http://') && !stristr($url, 'https://')) { + $url = 'http://' . $url; + } + $parts = parse_url($url); + if (!isset($parts['host'])) { + return FALSE; + } + + $host = $parts['host']; + if (stripos($host, 'www.') > -1) { + $host = substr($host, 4); + } + + $domains = _video_embed_field_get_provider_domains(); + $handlers = video_embed_get_handlers(); + if (isset($domains[$host])) { + $handler_name = $domains[$host]; + $handler = $handlers[$handler_name]; + $handler['name'] = $handler_name; + return $handler; + } + else { + return FALSE; + } +} + +/** + * Create a form from the player configuration options + * $defaults will be passed in with the default settings for the various fields + */ +function video_embed_field_get_form($defaults) { + $form = array(); + + $handlers = video_embed_get_handlers(); + + foreach ($handlers as $name => $handler) { + if (isset($handler['form']) && function_exists($handler['form'])) { + $handler_defaults = isset($defaults[$name]) ? $defaults[$name] : array(); + $handler_defaults = array_merge($handler['defaults'], $handler_defaults); + + $form[$name] = call_user_func($handler['form'], $handler_defaults); + + $form[$name] += array( + '#type' => 'fieldset', + '#title' => t($handler['title']), + '#tree' => TRUE, + ); + } + } + + return $form; +} + +/** + * Get an array of image styles suitable for using as select list options. + * + * @param $include_empty + * If TRUE a option will be inserted in the options array. + * @return + * Array of image styles both key and value are set to style name. + */ +function video_embed_field_video_style_options($include_empty = TRUE) { + $styles = video_embed_field_video_styles(); + $options = array(); + if ($include_empty && !empty($styles)) { + $options[''] = t(''); + } + $options = array_merge($options, drupal_map_assoc(array_keys($styles))); + if (empty($options)) { + $options[''] = t('No defined styles'); + } + return $options; +} + +/** + * Implements hook_filter_info(). + */ +function video_embed_field_filter_info() { + $filters['video_embed_field'] = array( + 'title' => t('Video Embedding'), + 'description' => t('Replaces [VIDEO::http://www.youtube.com/watch?v=someVideoID::aVideoStyle] tags with embedded videos.'), + 'process callback' => 'video_embed_field_filter_process', + ); + + return $filters; +} + +/** + * Video Embed Field filter process callback. + */ +function video_embed_field_filter_process($text, $filter, $format) { + preg_match_all('/ \[VIDEO:: ( [^\[\]]+ )* \] /x', $text, $matches); + + $tag_match = (array) array_unique($matches[1]); + $handlers = video_embed_get_handlers(); + + foreach ($tag_match as $tag) { + $parts = explode('::', $tag); + + // Get video style + if (isset($parts[1])) { + $style = $parts[1]; + } + else { + $style = 'normal'; + } + + $embed_code = theme('video_embed_field_embed_code', array('url' => $parts[0], 'style' => $style)); + + $text = str_replace('[VIDEO::' . $tag . ']', $embed_code, $text); + } + + return $text; +} + +/** + * Process variables to format a video player. + * + * $variables contains the following information: + * - $url + * - $style + * - $video_data + * + * @see video-embed.tpl.php + */ +function template_preprocess_video_embed_field_embed_code(&$variables) { + // Get the handler + $handler = video_embed_get_handler($variables['url']); + $variables['handler'] = $handler['name']; + + // Load the style + $style = video_embed_field_video_style_load($variables['style']); + // If there was an issue load in the default style + if ($style == FALSE) { + $style = video_embed_field_video_style_load('normal'); + } + if (isset($style->data[$variables['handler']])) { + $variables['style_settings'] = $style->data[$variables['handler']]; + } //safety valve for when we add new handlers and there are styles in the database. + else { + $variables['style_settings'] = $handler['defaults']; + } + + + // Prepare the URL + if (!stristr($variables['url'], 'http://') && !stristr($variables['url'], 'https://')) { + $variables['url'] = 'http://' . $variables['url']; + } + + // Prepare embed code + if ($handler && isset($handler['function']) && function_exists($handler['function'])) { + $embed_code = call_user_func($handler['function'], $variables['url'], $variables['style_settings']); + $variables['embed_code'] = drupal_render($embed_code); + } + else { + $variables['embed_code'] = l($variables['url'], $variables['url']); + } + + // Prepare video data + $variables['data'] = $variables['video_data']; + unset($variables['video_data']); +} + +/** + * Returns image style image with a link to + * an embedded video in colorbox. + * + * @param $variables + * An associative array containing: + * - image_url: The image URL. + * - image_style: The image style to use. + * - video_url: The video URL. + * - video_style: The video style to use. + * - video_data: An array of data about the video. + * + * @ingroup themeable + */ +function theme_video_embed_field_colorbox_code($variables) { + $style = video_embed_field_video_style_load($variables['video_style']); + + // If there was an issue load in the default style + if ($style == FALSE) { + $style = video_embed_field_video_style_load('normal'); + } + + $handler = video_embed_get_handler($variables['video_url']); + + $data = $style->data[$handler['name']]; + + //Create a unique ID for colorbox inline + $id = uniqid('video_embed_field-' . rand()); + + if ($variables['image_style'] == 'none') { + $image = array( + array( + '#theme' => 'image', + '#path' => $variables['image_url'], + ), + ); + } + else { + $image = array( + '#theme' => 'image_style', + '#path' => $variables['image_url'], + '#style_name' => $variables['image_style'], + ); + } + + $image = drupal_render($image); + + // Write values for later AJAX load + $hash = _video_embed_field_store_video($variables['video_url'], $variables['video_style']); + + $output = l($image, base_path() . '?q=vef/load/' . $hash . '&width=' . ($data['width']) . '&height=' . ($data['height'] + 3), array('html' => TRUE, 'external' => TRUE, 'attributes' => array('class' => array('colorbox-load')))); + + return $output; +} + +/** + * Get the thumbnail url for a given video url + * @param $url - the url of the video + * @return a string representing the url of the thumbnail, or FALSE on error + */ +function video_embed_field_thumbnail_url($url) { + $handler = video_embed_get_handler($url); + if ($handler && isset($handler['thumbnail_function']) && function_exists($handler['thumbnail_function'])) { + $info = call_user_func($handler['thumbnail_function'], $url); + $info['handler'] = $handler['name']; + return $info; + } + return FALSE; +} + +/** + * Get a video data array for a given video url + * + * @param string $url + * A video URL of the data array you want returned + * + * @return array|false $data + * An array of video data, or FALSE on error + */ +function video_embed_field_get_video_data($url) { + $handler = video_embed_get_handler($url); + if ($handler && isset($handler['data_function']) && function_exists($handler['data_function'])) { + $data = call_user_func($handler['data_function'], $url); + $data['handler'] = $handler['name']; + return $data; + } + return FALSE; +} + +/** + * Fetch all available provider domains. + */ +function _video_embed_field_get_provider_domains() { + $domains = array(); + + $handlers = video_embed_get_handlers(); + foreach ($handlers as $name => $handler) { + if (isset($handler['function']) && function_exists($handler['function'])) { + foreach ($handler['domains'] as $domain) { + $domains[$domain] = $name; + } + } + } + + return $domains; +} + +/** + * Fetch settings string + */ +function _video_embed_code_get_settings_str($settings = array()) { + $values = array(); + + foreach ($settings as $name => $value) { + if (empty($value)) { + $values[] = $name; + } + else { + $values[] = $name . '=' . $value; + } + } + + return implode('&', $values); +} + +//used to array filter in video_embed_field_requirements +function _video_embed_field_array_filter($item) { + return (isset($item['type']) && $item['type'] == 'video_embed_field'); +} + +/** + * Store a video to be loaded later from an _video_embed_field_load_video + */ +function _video_embed_field_store_video($video_url, $video_style) { + //create a hash key + $hash = _video_embed_field_hash($video_url, $video_style); + + //check that is record doesn't already exist before saving it + if (!_video_embed_field_load_video($hash)) { + $record = array( + 'vhash' => $hash, + 'video_url' => $video_url, + 'video_style' => $video_style, + ); + + cache_set('vef-store-' . $hash, $record); + + //add it to our static cache so we won't have to go to the database + $static_cache = &drupal_static('vef_video_store', array()); + $static_cache[$hash] = $record; + } + return $hash; +} + +/** + * Callback to render a video for an Ajax call + */ +function _video_embed_field_show_video($hash) { + $data = _video_embed_field_load_video($hash); + $video = array( + '#theme' => 'video_embed_field_embed_code', + '#style' => $data['video_style'], + '#url' => $data['video_url'], + ); + + print drupal_render($video); + return NULL; +} + +/** + * Loads a video from the video store given its hash + * Returns either the data - an array with hash, video_url and video_style keys + */ +function _video_embed_field_load_video($hash) { + $static_cache = &drupal_static('vef_video_store', array()); + //check if we've already loaded it + if (isset($static_cache[$hash])) { + return $static_cache[$hash]; + } + else { + + $result = cache_get('vef-store-' . $hash); + if ($result) { + //cache it before returning + $data = $result->data; + $static_cache[$hash] = $data; + return $data; + } + else { + return FALSE; + } + } +} + +/** + * Creates a hash for storing or looking up a video in the store table + */ +function _video_embed_field_hash($video_url, $video_style) { + return md5('vef' . $video_url . $video_style); +} + diff --git a/sites/all/modules/features/admin/admin.features.filter.inc b/sites/all/modules/features/admin/admin.features.filter.inc new file mode 100644 index 0000000..d51ce0b --- /dev/null +++ b/sites/all/modules/features/admin/admin.features.filter.inc @@ -0,0 +1,69 @@ + 'filtred_html', + 'name' => 'Filtred html', + 'cache' => 1, + 'status' => 1, + 'weight' => 0, + 'filters' => array( + 'filter_html' => array( + 'weight' => -10, + 'status' => 1, + 'settings' => array( + 'allowed_html' => '