Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / EntityLink.php
index 2cd402cd0070a687cb6c6550f914402d9c107d8a..75b8da394949a76b70ab18c326fa4e4c40b00cac 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Drupal\views\Plugin\views\field;
 
+use Drupal\Core\Form\FormStateInterface;
 use Drupal\views\ResultRow;
 
 /**
@@ -20,12 +21,22 @@ class EntityLink extends LinkBase {
     return $this->getEntity($row) ? parent::render($row) : [];
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function renderLink(ResultRow $row) {
+    if ($this->options['output_url_as_text']) {
+      return $this->getUrlInfo($row)->toString();
+    }
+    return parent::renderLink($row);
+  }
+
   /**
    * {@inheritdoc}
    */
   protected function getUrlInfo(ResultRow $row) {
     $template = $this->getEntityLinkTemplate();
-    return $this->getEntity($row)->urlInfo($template);
+    return $this->getEntity($row)->toUrl($template)->setAbsolute($this->options['absolute']);
   }
 
   /**
@@ -45,4 +56,34 @@ class EntityLink extends LinkBase {
     return $this->t('view');
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function defineOptions() {
+    $options = parent::defineOptions();
+    $options['output_url_as_text'] = ['default' => FALSE];
+    $options['absolute'] = ['default' => FALSE];
+    return $options;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
+    $form['output_url_as_text'] = [
+      '#type' => 'checkbox',
+      '#title' => $this->t('Output the URL as text'),
+      '#default_value' => $this->options['output_url_as_text'],
+    ];
+    $form['absolute'] = [
+      '#type' => 'checkbox',
+      '#title' => $this->t('Use absolute link (begins with "http://")'),
+      '#default_value' => $this->options['absolute'],
+      '#description' => $this->t('Enable this option to output an absolute link. Required if you want to use the path as a link destination.'),
+    ];
+    parent::buildOptionsForm($form, $form_state);
+    // Only show the 'text' field if we don't want to output the raw URL.
+    $form['text']['#states']['visible'][':input[name="options[output_url_as_text]"]'] = ['checked' => FALSE];
+  }
+
 }