You are here

class ExampleNodeLabel in Extra Field Settings Provider 8

Same name and namespace in other branches
  1. 8.2 modules/extra_field_plus_example/src/Plugin/ExtraField/Display/ExampleNodeLabel.php \Drupal\extra_field_plus_example\Plugin\ExtraField\Display\ExampleNodeLabel

Example Extra field Display.

Plugin annotation


@ExtraFieldDisplay(
  id = "example_node_label",
  label = @Translation("Example Node Label"),
  bundles = {
    "node.*"
  },
  visible = false
)

Hierarchy

Expanded class hierarchy of ExampleNodeLabel

File

modules/extra_field_plus_example/src/Plugin/ExtraField/Display/ExampleNodeLabel.php, line 20

Namespace

Drupal\extra_field_plus_example\Plugin\ExtraField\Display
View source
class ExampleNodeLabel extends ExtraFieldPlusDisplayBase {

  /**
   * {@inheritdoc}
   */
  public function view(ContentEntityInterface $entity) {
    $settings = $this
      ->getSettings();
    $link_to_entity = $settings['link_to_entity'];
    $wrapper = $settings['wrapper'];
    $label = $entity
      ->label();
    $url = NULL;
    if ($link_to_entity) {
      $url = $entity
        ->toUrl()
        ->setAbsolute();
    }
    if ($url) {
      $element = [
        '#type' => 'html_tag',
        '#tag' => $wrapper,
        [
          '#type' => 'link',
          '#title' => $label,
          '#url' => $url,
        ],
      ];
    }
    else {
      $element = [
        '#type' => 'html_tag',
        '#tag' => $wrapper,
        '#value' => $label,
      ];
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm() {
    $form = parent::settingsForm();
    $form['link_to_entity'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Link to the entity'),
    ];
    $form['wrapper'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Wrapper'),
      '#options' => [
        'span' => 'span',
        'p' => 'p',
        'h1' => 'h1',
        'h2' => 'h2',
        'h3' => 'h3',
        'h4' => 'h4',
        'h5' => 'h5',
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultFormValues() {
    $values = parent::defaultFormValues();
    $values += [
      'link_to_entity' => FALSE,
      'wrapper' => 'span',
    ];
    return $values;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExampleNodeLabel::defaultFormValues public function Provides field settings form default values. Overrides ExtraFieldPlusDisplayBase::defaultFormValues
ExampleNodeLabel::settingsForm public function Provides field settings form. Overrides ExtraFieldPlusDisplayBase::settingsForm
ExampleNodeLabel::view public function
ExtraFieldPlusDisplayBase::getDefaultFormValues public function Returns field settings form default values. Overrides ExtraFieldPlusDisplayInterface::getDefaultFormValues
ExtraFieldPlusDisplayBase::getSetting public function Returns field setting. Overrides ExtraFieldPlusDisplayInterface::getSetting
ExtraFieldPlusDisplayBase::getSettings public function Returns field settings. Overrides ExtraFieldPlusDisplayInterface::getSettings
ExtraFieldPlusDisplayBase::getSettingsForm public function Returns field settings form. Overrides ExtraFieldPlusDisplayInterface::getSettingsForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.