Closed (fixed)
Project:
Add To Calendar Button (AddEvent.com)
Version:
8.x-2.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
2 Aug 2017 at 19:28 UTC
Updated:
30 Jun 2023 at 09:51 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
netsliverHello,
I had the same need, I tried a modification to create a pseudo field, attaching the patch. It's probably improveable.
Comment #3
netsliverWith an additional class in the field
Comment #4
gagarine commentedComment #5
gagarine commentedAfter appying the patch I got :
Notice: Undefined index: separated_field in _addtocalendar_preprocess_field() (line 175 of modules/contrib/addtocalendar/includes/addtocalendar.build.inc).I didn't add a pseudo field yet but I do have "add to calendar" widget on the page.
Comment #6
netsliverHello,
On the formatter of the date field I added a parameter field to indicate that you want the separate button, it is it that is not initialized in your case.
Regards
Comment #7
gagarine commentedThanks for your reply. I guess we can avoid this error in the case the user didn't tick the parameter to activate the separate button.
Comment #8
purushotam.rai commentedGoing through above comments, I feel we need dedicated field formatter for Add to Calendar besides the current architecture supporting third-party field formatter settings.
We already have thought about this and there is issue open for this: #2819970: Add Separate Field Formatter for Addtocalendar
Let me know if we are on the same page.
Comment #9
guptahemant commentedComment #12
gg24 commentedHi @guptahemant,
I have tested this functionality. This works as expected.
Comment #13
gg24 commentedMarking this as fixed. Thanks for the effort guys!
Comment #15
Mimo Dia commentedWe have two methodes to solve the issue that is how to no display the bottom of the module addtocalendar if the event date has passed.
- First methode is to use preprocess hook on field(modulename_preprocess_field) in the modulename.module file.
So use the following code and adapted to your module name and the content type name:
////////////////// //////////////////////////////////////////////////
function myModuleName_preprocess_field(&$variables) {
//if we have field_date and thedate is the date of the content type (here content type can be for exemple "event")
if($variables['element']['#field_name'] == "field_date" && $variables['element']['#bundle'] == "myContentTypeName") {
// unset($variables['element']['#attached']);
// kint( $variables['element'] );
$date = strtotime($variables['element']['0']['#attributes']['datetime']);
// If the date of the event is less than the date of today (current date)
if($date < time()) {
unset($variables['#attached']);
unset($variables['#attached']);
unset($variables['items'][0]['content']['#suffix']);
}
}
}
Comment #16
Mimo Dia commentedThe second solution is to work with the preprocess page. And then pass the value of the field of date from drupal to your module js file.
So we have 3 steps to do, in order to solve this issue
1- Create your library file
------------moduleName.libraries.yml----------
participate:
version: VERSION
js:
js/moduleName.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
------------------------------------------------------------------
2-
- Select your content type that contains the field_date and get this value.
- attached your librarie in this specific content type
- Pass the value field date value to your js file (moduleName.js)
-----------moduleName.module ------------------------------
function moduleName_page_attachments(array &$page){
$page['#attached']['library'][]= 'moduleName/participate';
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
if($node->getType()=='nameOfYourContentType'){
$dateFieldValue= $node->field_date->value;
// Pass the value field date value to your js file (moduleName.js)
$page['#attached']['drupalSettings']['moduleName']['participate']['dateFieldValue'] = $dateFieldValue;
}
}
}
------------------------------ -----------------------------------
3- Remove the adToCalendar module class, if the event date is less that the current date
------------------------ moduleName.js ---------------------------------
var now = new Date();
(function($, Drupal, drupalSettings){
$(document).ready(function(){
dateFieldValue= new Date(drupalSettings.moduleName.participate.dateFieldValue);
if(dateFieldValue< now){
$('.addtocalendar').remove();
}
});
})(jQuery, Drupal, drupalSettings);
------------------------------------------------------------------------
Comment #17
netsliverPatch for latest dev 3.x version
Comment #18
netslivernew patch to fix error : [error] "show_separated_button" is an invalid render array key Element.php:98
Comment #19
netsliverpatch rerolled with last dev 3.x version