Add to calendar is linked with a date field. This is problematic if you want to display the "add to calendar" button at an other place than the date.

Would be more flexible to have the possibility to add "add to calendar" field that store no data but where you can choose which date field to use.

Comments

gagarine created an issue. See original summary.

netsliver’s picture

Status: Active » Needs work
StatusFileSize
new6.91 KB

Hello,

I had the same need, I tried a modification to create a pseudo field, attaching the patch. It's probably improveable.

netsliver’s picture

StatusFileSize
new6.91 KB

With an additional class in the field

gagarine’s picture

Status: Needs work » Needs review
gagarine’s picture

After 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.

netsliver’s picture

Hello,

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

gagarine’s picture

Status: Needs review » Needs work

Thanks 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.

purushotam.rai’s picture

Going 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.

guptahemant’s picture

Assigned: Unassigned » guptahemant

  • guptahemant committed 640cf23 on 8.x-3.x
    Issue #2899437 by guptahemant: Add to calendar as a field
    

  • guptahemant committed e52a0ee on 8.x-3.x
    Issue #2899437 by guptahemant: added the code to make disabled text...
gg24’s picture

Assigned: guptahemant » gg24
Status: Needs work » Reviewed & tested by the community

Hi @guptahemant,

I have tested this functionality. This works as expected.

gg24’s picture

Assigned: gg24 » Unassigned
Status: Reviewed & tested by the community » Fixed

Marking this as fixed. Thanks for the effort guys!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Mimo Dia’s picture

We 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']);

}
}
}

Mimo Dia’s picture

The 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);

------------------------------------------------------------------------

netsliver’s picture

StatusFileSize
new6.92 KB

Patch for latest dev 3.x version

netsliver’s picture

StatusFileSize
new6.92 KB

new patch to fix error : [error] "show_separated_button" is an invalid render array key Element.php:98

netsliver’s picture

StatusFileSize
new6.95 KB

patch rerolled with last dev 3.x version