Skip to content

Commit

Permalink
Backdrop port
Browse files Browse the repository at this point in the history
  • Loading branch information
robertgarrigos committed Aug 25, 2018
1 parent 0768806 commit 1761447
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 96 deletions.
339 changes: 339 additions & 0 deletions LICENSE.txt

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Viewfield
======================

Defines a field type to display a view.

Nodes hold content. Views save queries. Wouldn't be great if a node could hold a
saved query? Now it can.

Viewfield is a field type module that allows to render a particular view within
a field of an entity. When creating an entity, users can select the view to
render from a list of views. When the entity is displayed, the view is run and
the content is inserted into the field.

Installation
------------

- Install this module using the official Backdrop CMS instructions at
https://backdropcms.org/guide/modules.

Documentation
-------------

Additional documentation is located in the Wiki:
https://github.com/backdrop-contrib/viewfield/wiki/Documentation.

Issues
------

Bugs and Feature requests should be reported in the Issue Queue:
https://github.com/backdrop-contrib/viewfield/issues.

Current Maintainers
-------------------

- Robert Garrigos (https://github.com/robertgarrigos).

Credits
-------

- Ported to Backdrop CMS by Robert Garrigos (https://github.com/robertgarrigos).
- Originally written for Drupal by:
* Keith Morgan (keithm) - http://drupal.org/user/691250
* Daniel F. Kudwien (sun) - http://drupal.org/user/54136
* Jeremiah Davis (jerdavis) - http://drupal.org/user/228997
* Mark Fredrickson (mfredrickson) - http://drupal.org/user/31994

License
-------

This project is GPL v2 software. See the LICENSE.txt file in this directory for
complete text.

39 changes: 0 additions & 39 deletions README.txt

This file was deleted.

3 changes: 3 additions & 0 deletions config/viewfield.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"_config_name": "viewfield.settings"
}
4 changes: 2 additions & 2 deletions viewfield.info
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = Viewfield
description = Defines a field type to display a view.
core = 7.x
backdrop = 1.x
type = module
package = Fields
dependencies[] = views
files[] = tests/viewfield.test
63 changes: 19 additions & 44 deletions viewfield.install
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,29 @@ function viewfield_field_schema($field) {
);
}

// TODO The old hook_update_N functions cannot be applied to Backdrop.
function viewfield_update_7200() { }

// TODO The old hook_update_N functions cannot be applied to Backdrop.
function viewfield_update_7201() { }

/**
* Implements hook_update_last_removed().
*/
function viewfield_update_last_removed() {
return 7201;
}

/**
* Migrate force_default value from widget to instance settings.
* Implements hook_update_N().
*/
function viewfield_update_7200() {
$result = db_query("SELECT fci.id, fci.data FROM {field_config} fc INNER JOIN {field_config_instance} fci ON fc.id = fci.field_id WHERE fc.type = 'viewfield'");
foreach ($result as $record) {
$data = unserialize($record->data);
if (isset($data['widget']['settings']['force_default'])) {
// Protect against the case where a user has upgraded viewfield, produced
// a new instance-level force_default, and only later runs update.php.
if (!isset($data['settings']['force_default'])) {
$data['settings']['force_default'] = $data['widget']['settings']['force_default'];
}
unset($data['widget']['settings']['force_default']);
db_update('field_config_instance')
->fields(array('data' => serialize($data)))
->condition('id', $record->id)
->execute();
}
}
function viewfield_update_1000() {
$config = config('viewfield.settings');
}

/**
* Migrate allowed_views value from field to instance settings.
* Implements hook_install().
*/
function viewfield_update_7201() {
$fields = db_query("SELECT id, data FROM {field_config} WHERE type = 'viewfield'");
foreach ($fields as $field) {
$fc_data = unserialize($field->data);
if (isset($fc_data['settings']['allowed_views'])) {
$instances = db_query("SELECT id, data FROM {field_config_instance} WHERE field_id = :field_id", array(':field_id' => $field->id));
foreach ($instances as $instance) {
$fci_data = unserialize($instance->data);
// Write new setting only if a new one hasn't been created before
// running this update.
if (!isset($fci_data['settings']['allowed_views'])) {
$fci_data['settings']['allowed_views'] = $fc_data['settings']['allowed_views'];
db_update('field_config_instance')
->fields(array('data' => serialize($fci_data)))
->condition('id', $instance->id)
->execute();
}
}
unset($fc_data['settings']['allowed_views']);
db_update('field_config')
->fields(array('data' => serialize($fc_data)))
->condition('id', $field->id)
->execute();
}
}
function viewfield_install() {
// Dynamically generated variable data was detected.
}
22 changes: 11 additions & 11 deletions viewfield.module
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function viewfield_field_instance_settings_form($field, $instance) {
$form['allowed_views'] = array(
'#type' => 'checkboxes',
'#title' => t('Allowed values'),
'#options' => drupal_map_assoc(array_keys(views_get_enabled_views())),
'#options' => backdrop_map_assoc(array_keys(views_get_enabled_views())),
'#default_value' => isset($instance['settings']['allowed_views']) && is_array($instance['settings']['allowed_views']) ? $instance['settings']['allowed_views'] : array(),
'#description' => t('Only selected views will be available for content authors. Leave empty to allow all.'),
);
Expand Down Expand Up @@ -112,7 +112,7 @@ function viewfield_field_formatter_info() {
return array(
'viewfield_default' => array(
'label' => t('Default'),
'field types' => array('viewfield')
'field types' => array('viewfield'),
),
);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ function viewfield_element_info() {
* @see viewfield_post_render()
*/
function viewfield_pre_render($element) {
$stack = &drupal_static('viewfield_stack', array());
$stack = &backdrop_static('viewfield_stack', array());

// Abort rendering in case the view could not be loaded.
if (empty($element['#view'])) {
Expand Down Expand Up @@ -198,7 +198,7 @@ function viewfield_pre_render($element) {
* @see viewfield_field_formatter_view()
*/
function viewfield_post_render($content, $element) {
$stack = &drupal_static('viewfield_stack', array());
$stack = &backdrop_static('viewfield_stack', array());

unset($stack[$element['#entity_type']][$element['#entity_id']]);

Expand Down Expand Up @@ -289,7 +289,7 @@ function viewfield_field_widget_form(&$form, &$form_state, $field, $instance, $l
// @todo Core: Fix bogus white-space: nowrap.
// @see http://drupal.org/node/1230336
$element['#attached']['css'] = array(
drupal_get_path('module', 'viewfield') . '/viewfield.css' => array(
backdrop_get_path('module', 'viewfield') . '/viewfield.css' => array(
'weight' => 1,
),
);
Expand Down Expand Up @@ -372,12 +372,12 @@ function _viewfield_get_view_args($vargs, $entity_type, $entity) {
* Implements hook_content_migrate_field_alter().
*/
function viewfield_content_migrate_field_alter(&$field_value, $instance_value) {
switch ($field_value['type']) {
case 'viewfield':
// allowed_views is now an instance setting.
unset($field_value['settings']['allowed_views']);
break;
}
switch ($field_value['type']) {
case 'viewfield':
// allowed_views is now an instance setting.
unset($field_value['settings']['allowed_views']);
break;
}
}

/**
Expand Down

0 comments on commit 1761447

Please sign in to comment.