diff --git a/addtoany.install b/addtoany.install new file mode 100644 index 0000000000000000000000000000000000000000..c535289e18509f2c46994f9bba977017944ca528 --- /dev/null +++ b/addtoany.install @@ -0,0 +1,119 @@ + ['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 595bea1cc43b612ad54d3e56fec7953a94c81fee..5b92435db461a7d638c9618035aa973c132c6a46 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(). @@ -24,35 +25,38 @@ 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 94fe969bf4d458fc6e7477e5652fa2eca5772689..a66611b94d916005148f1a7f6749716711f84221 100644 --- a/config/install/addtoany.settings.yml +++ b/config/install/addtoany.settings.yml @@ -5,12 +5,6 @@ additional_html: | 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 ea4eda62bad61219bb212884daecbab5b32f8c7e..a39c6bab558f6741371e71fcd3d4a3fe86cb89a4 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 267d1db5bf0bc9f95078c502d702365771b78a7b..d0d61befc6affc424717d88081eb80d529683d51 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 251709e71206fdc40076c6a589d1887b1ab2b2a5..0000000000000000000000000000000000000000 --- a/src/Plugin/DsField/AddToAnyField.php +++ /dev/null @@ -1,38 +0,0 @@ -getParameter('node'); - - return array( - '#addtoany_html' => addtoany_create_node_buttons($node), - '#theme' => 'addtoany_standard', - '#cache' => array( - 'contexts' => array('url'), - ), - ); - } -}