summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormarcvangend2016-09-15 17:58:38 -0700
committermicropat2016-09-15 17:58:38 -0700
commite223eb6ea40985aeb7073f9b961ef8749fb0b9cf (patch)
treec685e01f7261dea44954d323d048bbd37bee61a2
parent95056b254be48fd65857385798897a16d0c3cd17 (diff)
Issue #2772201 by marcvangend: Add true view mode support
-rw-r--r--addtoany.install119
-rw-r--r--addtoany.module38
-rw-r--r--config/install/addtoany.settings.yml6
-rw-r--r--config/schema/addtoany.schema.yml14
-rw-r--r--src/Form/AddToAnySettingsForm.php45
-rw-r--r--src/Plugin/DsField/AddToAnyField.php38
6 files changed, 140 insertions, 120 deletions
diff --git a/addtoany.install b/addtoany.install
new file mode 100644
index 0000000..c535289
--- /dev/null
+++ b/addtoany.install
@@ -0,0 +1,119 @@
+<?php
+
+/**
+ * @file
+ * Install, update and uninstall functions for the AddToAny module.
+ */
+
+use Drupal\Core\Entity\Entity\EntityViewDisplay;
+
+/**
+ * Implements hook_install().
+ */
+function addtoany_install() {
+ // Build a structured list of node bundles and view modes that have AddToAny
+ // enabled by default.
+ $node_enable_by_default = [
+ 'article' => ['default', 'teaser'],
+ 'page' => ['default', 'teaser'],
+ ];
+ // Get the config entity storage handler.
+ $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
+ foreach ($node_enable_by_default as $bundle => $viewmodes) {
+ foreach ($viewmodes as $viewmode) {
+ // Get the config entity for this bundle and view mode.
+ $display = $storage->load('node.' . $bundle . '.' . $viewmode);
+ // Enable the AddToAny extra field and save the config.
+ $display->setComponent('addtoany')->save();
+ }
+ }
+}
+
+/**
+ * Migrate global placement settings to settings per view mode.
+ */
+function addtoany_update_8101(&$sandbox) {
+ $config_factory = \Drupal::configFactory();
+
+ // Gather the settings we need to migrate.
+ $addtoany_settings = $config_factory->getEditable('addtoany.settings');
+ $node_types = $addtoany_settings->getOriginal('nodetypes');
+ $display_in_teasers = $addtoany_settings->getOriginal('display_in_teasers');
+ $display_in_nodecont = $addtoany_settings->getOriginal('display_in_nodecont');
+ $display_weight = $addtoany_settings->getOriginal('display_weight');
+
+ // The entity display repository stores which view modes are enabled.
+ $entity_display_repository = \Drupal::getContainer()->get('entity_display.repository');
+
+ // Check if AddToAny is currently shown on at least one node type.
+ if (!empty($node_types) && $display_in_nodecont) {
+
+ // Loop over the AddToAny-enabled node types.
+ foreach ($node_types as $bundle) {
+ // Get enabled view modes.
+ $view_modes = $entity_display_repository->getViewModeOptionsByBundle('node', $bundle);
+
+ // If the teaser view mode is enabled but the old settings excluded it,
+ // take it out.
+ if (!empty($view_modes['teaser']) && $display_in_teasers == FALSE) {
+ unset($view_modes['teaser']);
+ }
+
+ // Loop over the view modes and apply new settings to match the old.
+ foreach ($view_modes as $view_mode => $view_mode_info) {
+ $display = EntityViewDisplay::load('node.' . $bundle . '.' . $view_mode);
+ $display->setComponent('addtoany', ['weight' => $display_weight])->save();
+ }
+ }
+ }
+
+ // Convert Display Suite fields to regular "extra fields".
+ if (\Drupal::moduleHandler()->moduleExists('ds')) {
+ // The DsField implementation only supported node entities.
+ $all_node_types = \Drupal\node\Entity\NodeType::loadMultiple();
+
+ foreach ($all_node_types as $node_type) {
+ // Get enabled view modes.
+ $view_modes = $entity_display_repository->getViewModeOptionsByBundle('node', $node_type->id());
+
+ // Loop over the view modes and apply new settings to match the old.
+ foreach ($view_modes as $view_mode => $view_mode_info) {
+ // Get the entity view display config entity.
+ $display = EntityViewDisplay::load('node.' . $node_type->id() . '.' . $view_mode);
+
+ // Check if this view mode uses Display Suite fields and if the Display
+ // Suite AddToAny field ("addtoany_field") was enabled.
+ $ds_fields = $display->getThirdPartySetting('ds', 'fields');
+ if (!empty($ds_fields) && isset($ds_fields['addtoany_field'])) {
+ // Remove the DS field, add the extra field.
+ unset($ds_fields['addtoany_field']);
+ $display->setComponent('addtoany');
+ // Keep the rest of the DS fields.
+ $display->setThirdPartySetting('ds', 'fields', $ds_fields);
+
+ // Find where the DS field was placed and put the AddToAny extra field
+ // in its place.
+ $ds_regions = $display->getThirdPartySetting('ds', 'regions');
+ foreach ($ds_regions as $label => $ds_region_fields) {
+ $key = array_search('addtoany_field', $ds_region_fields);
+ if ($key !== FALSE) {
+ $ds_regions[$label][$key] = 'addtoany';
+ }
+ }
+ // Apply the altered region settings.
+ $display->setThirdPartySetting('ds', 'regions', $ds_regions);
+
+ // Save the view mode.
+ $display->save();
+ }
+ }
+ }
+ }
+
+ // Finally, clean up no longer used settings.
+ $addtoany_settings->clear('nodetypes')
+ ->clear('display_in_teasers')
+ ->clear('display_in_nodecont')
+ ->clear('display_weight')
+ ->save();
+}
diff --git a/addtoany.module b/addtoany.module
index 595bea1..5b92435 100644
--- a/addtoany.module
+++ b/addtoany.module
@@ -10,6 +10,7 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
+use Drupal\node\Entity\NodeType;
/**
* Implements hook_theme().
@@ -25,34 +26,37 @@ function addtoany_theme() {
}
/**
+ * Implements hook_entity_extra_field_info().
+ */
+function addtoany_entity_extra_field_info() {
+ $extra = array();
+
+ /** @var \Drupal\node\NodeTypeInterface $bundle */
+ foreach (NodeType::loadMultiple() as $bundle) {
+ $extra['node'][$bundle->id()]['display']['addtoany'] = array(
+ 'label' => t('AddToAny'),
+ 'description' => t('Share buttons by AddToAny'),
+ 'weight' => 5,
+ 'visible' => FALSE,
+ );
+ }
+
+ return $extra;
+}
+
+/**
* Implements hook_ENTITY_TYPE_view().
*/
function addtoany_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
- $config = \Drupal::config('addtoany.settings');
- $types = $config->get('nodetypes');
- $type = $node->getType();
- $published = $node->isPublished();
- $teaser = ($view_mode == 'teaser') ? TRUE : FALSE;
-
- // Display in content section?
- $show = (!empty($type) && in_array($type, $types, TRUE) && ($published) && ($config->get('display_in_nodecont'))
- && (
- (!$teaser) || ($teaser && $config->get('display_in_teasers'))
- )
- );
- $weight = $config->get('display_weight');
-
- if ($show) {
+ if ($display->getComponent('addtoany') && $node->isPublished()) {
$build['addtoany'] = array(
// Not using #markup because data attribute name gets mangled
// @see https://www.drupal.org/node/2609928
// '#markup' => addtoany_create_node_buttons($node),
'#addtoany_html' => addtoany_create_node_buttons($node),
'#theme' => 'addtoany_standard',
- '#weight' => $weight,
);
}
-
}
/**
diff --git a/config/install/addtoany.settings.yml b/config/install/addtoany.settings.yml
index 94fe969..a66611b 100644
--- a/config/install/addtoany.settings.yml
+++ b/config/install/addtoany.settings.yml
@@ -5,12 +5,6 @@ additional_html: |
<a class="a2a_button_google_plus"></a>
additional_css: ''
additional_js: ''
-nodetypes:
- - page
- - article
-display_in_teasers: true
-display_in_nodecont: true
-display_weight: 10
universal_button: default
custom_universal_button: ''
universal_button_placement: before
diff --git a/config/schema/addtoany.schema.yml b/config/schema/addtoany.schema.yml
index ea4eda6..a39c6ba 100644
--- a/config/schema/addtoany.schema.yml
+++ b/config/schema/addtoany.schema.yml
@@ -14,20 +14,6 @@ addtoany.settings:
additional_js:
type: string
label: 'Additional JS'
- nodetypes:
- type: sequence
- label: 'Node Types'
- sequence:
- type: string
- display_in_teasers:
- type: boolean
- label: 'Display in teasers'
- display_in_nodecont:
- type: boolean
- label: 'Display in content section'
- display_weight:
- type: integer
- label: 'Content Weight'
universal_button:
type: string
label: 'Universal Button Type'
diff --git a/src/Form/AddToAnySettingsForm.php b/src/Form/AddToAnySettingsForm.php
index 267d1db..d0d61be 100644
--- a/src/Form/AddToAnySettingsForm.php
+++ b/src/Form/AddToAnySettingsForm.php
@@ -126,51 +126,6 @@ class AddToAnySettingsForm extends ConfigFormBase {
),
);
- $form['addtoany_placement_settings'] = array(
- '#type' => 'details',
- '#title' => t('Placement'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- );
- $form['addtoany_placement_settings']['addtoany_nodetypes'] = array(
- '#type' => 'checkboxes',
- '#title' => t('Node types'),
- '#description' => t('Display buttons for these node types.'),
- '#default_value' => $addtoany_settings->get('nodetypes'),
- '#options' => node_type_get_names(),
- );
- $form['addtoany_placement_settings']['addtoany_display_in_teasers'] = array(
- '#type' => 'checkbox',
- '#title' => t('Display for node teasers'),
- '#default_value' => $addtoany_settings->get('display_in_teasers'),
- '#description' => t('Display buttons for node teasers in selected sections.'),
- '#states' => array(
- // Disable if no section placement is selected
- 'disabled' => array(
- ':input[name="addtoany_display_in_nodecont"]' => array('checked' => FALSE),
- ),
- ),
- );
- $form['addtoany_placement_settings']['addtoany_display_in_nodecont'] = array(
- '#type' => 'checkbox',
- '#title' => t('Display in content section'),
- '#default_value' => $addtoany_settings->get('display_in_nodecont'),
- '#description' => t('Display buttons in the content section of node pages.'),
- );
- $form['addtoany_placement_settings']['addtoany_display_weight'] = array(
- '#type' => 'weight',
- '#title' => t('Content weight'),
- '#default_value' => $addtoany_settings->get('display_weight'),
- '#delta' => 50,
- '#description' => t('Optional weight value for reordering AddToAny within the content section.'),
- '#states' => array(
- // Show only if placement in "content" selected
- 'visible' => array(
- ':input[name="addtoany_display_in_nodecont"]' => array('checked' => TRUE),
- ),
- ),
- );
-
$form['addtoany_additional_settings'] = array(
'#type' => 'details',
'#title' => t('Additional options'),
diff --git a/src/Plugin/DsField/AddToAnyField.php b/src/Plugin/DsField/AddToAnyField.php
deleted file mode 100644
index 251709e..0000000
--- a/src/Plugin/DsField/AddToAnyField.php
+++ /dev/null
@@ -1,38 +0,0 @@
-<?php
-
-/**
- * @file
- * Contains \Drupal\ds\Plugin\DsField\DsFieldBase.
- */
-
-namespace Drupal\addtoany\Plugin\DsField;
-use Drupal\ds\Plugin\DsField\DsFieldBase;
-
-/**
- * Plugin that renders the AddToAny Buttons as Display Suite Field.
- *
- * @DsField(
- * id = "addtoany_field",
- * title = @Translation("AddToAny Buttons"),
- * entity_type = "node",
- * provider = "addtoany",
- * ui_limit = {"*|*"}
- * )
- */
-class AddToAnyField extends DsFieldBase {
-
- /**
- * {@inheritdoc}
- */
- public function build() {
- $node = \Drupal::routeMatch()->getParameter('node');
-
- return array(
- '#addtoany_html' => addtoany_create_node_buttons($node),
- '#theme' => 'addtoany_standard',
- '#cache' => array(
- 'contexts' => array('url'),
- ),
- );
- }
-}