You are here

protected function AddToCalTypeBase::extractFieldValue in Add to Cal 8

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

$field_name:

Return value

string

1 call to AddToCalTypeBase::extractFieldValue()
AddToCalTypeBase::extractEventDetails in src/AddToCalTypeBase.php

File

src/AddToCalTypeBase.php, line 149

Class

AddToCalTypeBase

Namespace

Drupal\addtocal

Code

protected function extractFieldValue(EntityInterface $entity, $field_name) {
  $output = '';
  if (!empty($field_name)) {
    $value = $entity
      ->get($field_name)
      ->getValue();
    $instance = FieldStorageConfig::loadByName($entity
      ->getEntityTypeId(), $field_name);
    if ($instance
      ->getType() == 'address') {
      $address = $value[0];
      $string = '';
      if (!empty($address['address_line1'])) {
        $string .= $address['address_line1'] . ' ';
      }
      if (!empty($address['address_line2'])) {
        $string .= $address['address_line2'] . ', ';
      }
      if (!empty($address['locality'])) {
        $string .= $address['locality'] . ', ';
      }
      if (!empty($address['administrative_area'])) {
        $string .= $address['administrative_area'] . ' ';
      }
      if (!empty($address['postal_code'])) {
        $string .= $address['postal_code'] . ', ';
      }
      if (!empty($address['country_code'])) {
        $string .= $address['country_code'];
      }
      $output = $string;
    }
    else {
      $replace_strings = [
        ' ' => '',
        '<br />' => '',
        PHP_EOL => '',
      ];
      $output = $value[0]['value'];
      foreach ($replace_strings as $search => $replace) {
        $output = str_replace($search, $replace, $output);
      }
    }
  }
  return strip_tags($output);
}