getDefaultLabel example

if (!$this->currentUser) {
      $this->currentUser = \Drupal::currentUser();
    }
    return $this->currentUser;
  }

  /** * {@inheritdoc} */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['text'] = ['default' => $this->getDefaultLabel()];
    return $options;
  }

  /** * {@inheritdoc} */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $form['text'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Text to display'),
      '#default_value' => $this->options['text'],
    ];

class ContactLink extends LinkBase {

  /** * {@inheritdoc} */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form$form_state);
    $form['text']['#title'] = $this->t('Link label');
    $form['text']['#required'] = TRUE;
    $form['text']['#default_value'] = empty($this->options['text']) ? $this->getDefaultLabel() : $this->options['text'];
  }

  /** * {@inheritdoc} */
  protected function getUrlInfo(ResultRow $row) {
    $entity = $this->getEntity($row);
    if (!$entity) {
      return NULL;
    }
    return Url::fromRoute('entity.user.contact_form', ['user' => $entity->id()]);
  }
Home | Imprint | This part of the site doesn't use cookies.