You are here

public function BatTypeHandlerTypeCalendarsField::render in Booking and Availability Management Tools for Drupal 8

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

modules/bat_unit/src/Plugin/views/field/BatTypeHandlerTypeCalendarsField.php, line 61
This field handler aggregates calendar edit links for a Bat Type under a single field.

Class

BatTypeHandlerTypeCalendarsField
Plugin annotation @ViewsField("bat_type_handler_type_calendars_field");

Namespace

Drupal\bat_unit\Plugin\views\field

Code

public function render(ResultRow $values) {
  $links = [];
  $type = $this
    ->getEntity($values);
  $type_bundle = bat_type_bundle_load($type
    ->bundle());
  if (is_array($type_bundle->default_event_value_field_ids) && $this
    ->getModuleHandler()
    ->moduleExists('bat_event_ui')) {
    foreach ($type_bundle->default_event_value_field_ids as $event_type => $field) {
      if (!empty($field)) {
        $event_type_path = Url::fromRoute('bat_event_ui.calendar', [
          'unit_type' => $type
            ->id(),
          'event_type' => $event_type,
        ])
          ->toString();

        // Check if user has permission to access $event_type_path.
        if ($url_object = $this->pathValidator
          ->getUrlIfValid($event_type_path)) {
          $route_name = $url_object
            ->getRouteName();
          if (bat_event_get_types($event_type)) {
            $event_type_label = bat_event_get_types($event_type)
              ->label();
            $links[$event_type] = [
              'title' => t('Manage @event_type_label', [
                '@event_type_label' => $event_type_label,
              ]),
              'url' => Url::fromRoute($route_name, [
                'unit_type' => $type
                  ->id(),
                'event_type' => $event_type,
              ]),
            ];
          }
        }
      }
    }
  }
  if (!empty($links)) {
    return [
      '#type' => 'operations',
      '#links' => $links,
    ];
  }
  else {

    // Hide this field.
    $this->options['exclude'] = TRUE;
  }
}