Hello,

Similar to this issue issue, how it is possible to display only the start and end dates in a node page? e.g something like 12/01/2019-30/01/2019 (and not the dates between them) ? I thought that computed field might be a solution but I can not find what kind of code I should write inside it...

Thanks

Comments

m_dimitris created an issue. See original summary.

neslee canil pinto’s picture

Assigned: Unassigned » neslee canil pinto
dpi’s picture

You'd need to write your own field formatter and make use of getHelper API.

dpi’s picture

Issue tags: -node, -page rendering

d.o folks are very particular about useless tags. best use them for initiatives or grouping particular issues together.

neslee canil pinto’s picture

Assigned: neslee canil pinto » Unassigned
m_dimitris’s picture

Thaks very much for the help! If I understood correctly, do I need to create a new module and apply the field api on it?

Thanks again.

dpi’s picture

Version: 8.x-2.0-alpha1 » 8.x-1.x-dev
Status: Active » Fixed

That's right. Create a new formatter plugin in your own module

dpi’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
m_dimitris’s picture

Category: Feature request » Support request

Hello again,

Is there an example of a custom field formatter with helper API? I am trying to use helper in a custom module but I cant figure out how I could use it in viewElements functions.

Here is my code so far:

<?php

/**
 * @file
 * Contains Drupal\drupal8_field_formatters\Plugin\Field\FieldFormatter\DateRecurFormatter.
 */

namespace Drupal\drupal8_field_formatters\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\date_recur\DateRecurHelperInterface;

/**
 * Plugin implementation of the 'main_heading_formatter' formatter.
 *
 * @FieldFormatter(
 *   id = "date_recur_formatter",
 *   label = @Translation("Start and end date only"),
 *   weight = "10",
 *   field_types = {
 *     "date_recur"
 *   },
 *   quickedit = {
 *     "editor" = "plain_text"
 *   }
 * )
 */

class DateRecurFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    
  
    $element = [];

    foreach ($items as $delta => $item) {
      // Render each element as markup.
      $element[$delta] = [
        '#type' => 'markup',
        '#markup' => '<p>' . $item->value . '</p>',
      ];
    }

    return $element;
  }

}

Thanks for your help

m_dimitris’s picture

Status: Fixed » Active
dpi’s picture

Everything you need can be found in the docs: https://www.drupal.org/docs/8/modules/recurring-dates-field/date-recur-i... under "Interpreting Rules" section.

Access helper via $item

dpi’s picture

dpi’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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