TranslatableMarkup example

$this->assertSame($expected(string) $this->renderObjectWithTwig($variable));
  }

  /** * Provide test examples. */
  public function providerTestMarkupInterfaceEmpty() {
    return [
      // The first argument to \Drupal\Core\StringTranslation\TranslatableMarkup       // is not supposed to be an empty string.       // phpcs:ignore Drupal.Semantics.FunctionT.EmptyString       'empty TranslatableMarkup' => ['', new TranslatableMarkup('')],
      'non-empty TranslatableMarkup' => ['<span>test</span>', new TranslatableMarkup('test')],
      'empty FormattableMarkup' => ['', new FormattableMarkup('', ['@foo' => 'bar'])],
      'non-empty FormattableMarkup' => ['<span>bar</span>', new FormattableMarkup('@foo', ['@foo' => 'bar'])],
      'non-empty Markup' => ['<span>test</span>', Markup::create('test')],
      'empty GeneratedLink' => ['', new GeneratedLink()],
      'non-empty GeneratedLink' => ['<span><a hef="http://www.example.com">test</a></span>', (new GeneratedLink())->setGeneratedLink('<a hef="http://www.example.com">test</a>')],
      // Test objects that do not implement \Countable.       'empty SafeMarkupTestMarkup' => ['', SafeMarkupTestMarkup::create('')],
      'non-empty SafeMarkupTestMarkup' => ['<span>test</span>', SafeMarkupTestMarkup::create('test')],
    ];
  }

  
protected function setupBreakpoint() {
    $this->breakpoint = new Breakpoint([]$this->pluginId, $this->pluginDefinition);
    $this->breakpoint->setStringTranslation($this->stringTranslation);
  }

  /** * @covers ::getLabel */
  public function testGetLabel() {
    $this->pluginDefinition['label'] = 'Test label';
    $this->setupBreakpoint();
    $this->assertEquals(new TranslatableMarkup('Test label', []['context' => 'breakpoint']$this->stringTranslation)$this->breakpoint->getLabel());
  }

  /** * @covers ::getWeight */
  public function testGetWeight() {
    $this->pluginDefinition['weight'] = '4';
    $this->setupBreakpoint();
    // Assert that the type returned in an integer.     $this->assertSame(4, $this->breakpoint->getWeight());
  }

  

function hook_link_alter(&$variables) {
  // Add a warning to the end of route links to the admin section.   if (isset($variables['route_name']) && str_contains($variables['route_name'], 'admin')) {
    $variables['text'] = new TranslatableMarkup('@text (Warning!)', ['@text' => $variables['text']]);
  }
}

/** * @} End of "addtogroup hooks". */


  /** * Element validate; Check View is valid. */
  public static function settingsFormValidate($element, FormStateInterface $form_state$form) {
    // Split view name and display name from the 'view_and_display' value.     if (!empty($element['view_and_display']['#value'])) {
      [$view$display] = explode(':', $element['view_and_display']['#value']);
    }
    else {
      $form_state->setError($elementnew TranslatableMarkup('The views entity selection mode requires a view.'));
      return;
    }

    // Explode the 'arguments' string into an actual array. Beware, explode()     // turns an empty string into an array with one empty string. We'll need an     // empty array instead.     $arguments_string = trim($element['arguments']['#value']);
    if ($arguments_string === '') {
      $arguments = [];
    }
    else {
      
public $entity_type_class = 'Drupal\Core\Config\Entity\ConfigEntityType';

  /** * {@inheritdoc} */
  public $group = 'configuration';

  /** * {@inheritdoc} */
  public function get() {
    $this->definition['group_label'] = new TranslatableMarkup('Configuration', []['context' => 'Entity type group']);

    return parent::get();
  }

}
return $schema;
  }

  /** * Overrides \Drupal\views\Tests\ViewTestBase::viewsData(). * * Adds a relationship for the uid column. */
  protected function viewsData() {
    $data = parent::viewsData();
    $data['views_test_data']['uid'] = [
      'title' => new TranslatableMarkup('UID'),
      'help' => new TranslatableMarkup('The test data UID'),
      'relationship' => [
        'id' => 'standard',
        'base' => 'users_field_data',
        'base field' => 'uid',
      ],
    ];

    return $data;
  }

}

  public static function validateElement(array $element, FormStateInterface $form_state) {
    if ($element['#required'] && $element['#value'] == '_none') {
      if (isset($element['#required_error'])) {
        $form_state->setError($element$element['#required_error']);
      }
      else {
        $form_state->setError($elementnew TranslatableMarkup('@name field is required.', ['@name' => $element['#title']]));
      }
    }

    // Massage submitted form values.     // Drupal\Core\Field\WidgetBase::submit() expects values as     // an array of values keyed by delta first, then by column, while our     // widgets return the opposite.
    if (is_array($element['#value'])) {
      $values = array_values($element['#value']);
    }
    

class PasswordItem extends StringItem {

  /** * {@inheritdoc} */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('The hashed password'))
      ->setSetting('case_sensitive', TRUE);
    $properties['existing'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Existing password'));
    $properties['pre_hashed'] = DataDefinition::create('boolean')
      ->setLabel(new TranslatableMarkup('Determines if a password needs hashing'));

    return $properties;
  }

  /** * {@inheritdoc} */

class ListStringItem extends ListItemBase {

  /** * {@inheritdoc} */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Text value'))
      ->addConstraint('Length', ['max' => 255])
      ->setRequired(TRUE);

    return $properties;
  }

  /** * {@inheritdoc} */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      
return [
      'title' => DRUPAL_OPTIONAL,
      'link_type' => LinkItemInterface::LINK_GENERIC,
    ] + parent::defaultFieldSettings();
  }

  /** * {@inheritdoc} */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['uri'] = DataDefinition::create('uri')
      ->setLabel(new TranslatableMarkup('URI'));

    $properties['title'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Link text'));

    $properties['options'] = MapDataDefinition::create()
      ->setLabel(new TranslatableMarkup('Options'));

    return $properties;
  }

  /** * {@inheritdoc} */

  public static function publishedBaseFieldDefinitions(EntityTypeInterface $entity_type) {
    if (!is_subclass_of($entity_type->getClass(), EntityPublishedInterface::class)) {
      throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \Drupal\Core\Entity\EntityPublishedInterface.');
    }
    if (!$entity_type->hasKey('published')) {
      throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have a "published" entity key.');
    }

    return [
      $entity_type->getKey('published') => BaseFieldDefinition::create('boolean')
        ->setLabel(new TranslatableMarkup('Published'))
        ->setRevisionable(TRUE)
        ->setTranslatable(TRUE)
        ->setDefaultValue(TRUE),
    ];
  }

  /** * {@inheritdoc} */
  public function isPublished() {
    return (bool) $this->getEntityKey('published');
  }
'goldfinger',
        TRUE,
        TRUE,
      ],
      'string vs FormattableMarkup, equal' => [
        'goldfinger',
        new FormattableMarkup('goldfinger', []),
        TRUE,
        TRUE,
      ],
      'TranslatableMarkup vs FormattableMarkup, equal' => [
        new TranslatableMarkup('goldfinger'),
        new FormattableMarkup('goldfinger', []),
        TRUE,
        TRUE,
      ],
      'TranslatableMarkup vs string, not equal' => [
        new TranslatableMarkup('goldfinger'),
        'moonraker',
        TRUE,
        ComparisonFailure::class,
      ],
      'TranslatableMarkup vs int, equal' => [
        
$context['%entity_type'] = $entity_type_id;
        $context += Error::decodeException($throwable);
        \Drupal::logger('update')->error('Unable to update %entity_type %view due to error @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
        $sandbox[self::SANDBOX_KEY]['failed_entity_ids'][] = $entity->id();
      }
    }

    $sandbox['#finished'] = empty($sandbox[self::SANDBOX_KEY]['entities']) ? 1 : ($sandbox[self::SANDBOX_KEY]['count'] - count($sandbox[self::SANDBOX_KEY]['entities'])) / $sandbox[self::SANDBOX_KEY]['count'];
    if (!empty($sandbox[self::SANDBOX_KEY]['failed_entity_ids'])) {
      $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
      if (\Drupal::moduleHandler()->moduleExists('dblog')) {
        return new TranslatableMarkup('Updates failed for the entity type %entity_type, for %entity_ids. <a href=":url">Check the logs</a>.', [
          '%entity_type' => $entity_type->getLabel(),
          '%entity_ids' => implode(', ', $sandbox[self::SANDBOX_KEY]['failed_entity_ids']),
          ':url' => Url::fromRoute('dblog.overview')->toString(),
        ]);
      }
      else {
        return new TranslatableMarkup("Updates failed for the entity type %entity_type, for %entity_ids. Check the logs.", [
          '%entity_type' => $entity_type->getLabel(),
          '%entity_ids' => implode(', ', $sandbox[self::SANDBOX_KEY]['failed_entity_ids']),
        ]);
      }
    }

  public function getDefinedLanguageTypesInfo() {
    $this->definedLanguageTypesInfo = [
      LanguageInterface::TYPE_INTERFACE => [
        'name' => new TranslatableMarkup('Interface text'),
        'description' => new TranslatableMarkup('Order of language detection methods for interface text. If a translation of interface text is available in the detected language, it will be displayed.'),
        'locked' => TRUE,
      ],
      LanguageInterface::TYPE_CONTENT => [
        'name' => new TranslatableMarkup('Content'),
        'description' => new TranslatableMarkup('Order of language detection methods for content. If a version of content is available in the detected language, it will be displayed.'),
        'locked' => TRUE,
      ],
      LanguageInterface::TYPE_URL => [
        'locked' => TRUE,
      ],
    ];
->setLabel('Computed Field Test')
      ->setComputed(TRUE)
      ->setClass(ComputedTestFieldItemList::class);

    $fields['computed_reference_field'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel('Computed Reference Field Test')
      ->setComputed(TRUE)
      ->setSetting('target_type', 'entity_test')
      ->setClass(ComputedReferenceTestFieldItemList::class);

    $fields['computed_test_cacheable_string_field'] = BaseFieldDefinition::create('computed_test_cacheable_string_item')
      ->setLabel(new TranslatableMarkup('Computed Cacheable String Field Test'))
      ->setComputed(TRUE)
      ->setClass(ComputedTestCacheableStringItemList::class)
      ->setReadOnly(FALSE)
      ->setInternal(FALSE);

    return $fields;
  }

}
Home | Imprint | This part of the site doesn't use cookies.