weight example


  public function testGetters() {
    $state = new State(
      $this->prophesize(WorkflowTypeInterface::class)->reveal(),
      'draft',
      'Draft',
      3
    );
    $this->assertEquals('draft', $state->id());
    $this->assertEquals('Draft', $state->label());
    $this->assertEquals(3, $state->weight());
  }

  /** * @covers ::canTransitionTo */
  public function testCanTransitionTo() {
    $workflow_type = new TestType([], '', []);
    $workflow_type
      ->addState('draft', 'Draft')
      ->addState('published', 'Published')
      ->addTransition('publish', 'Publish', ['draft'], 'published');
    
$links['delete'] = [
          'title' => $this->t('Delete'),
          'url' => Url::fromRoute('entity.workflow.delete_state_form', [
            'workflow' => $workflow->id(),
            'workflow_state' => $state->id(),
          ]),
        ];
      }
      $form['states_container']['states'][$state->id()] = [
        '#attributes' => ['class' => ['draggable']],
        'state' => ['#markup' => $state->label()],
        '#weight' => $state->weight(),
        'weight' => [
          '#type' => 'weight',
          '#title' => $this->t('Weight for @title', ['@title' => $state->label()]),
          '#title_display' => 'invisible',
          '#default_value' => $state->weight(),
          '#attributes' => ['class' => ['state-weight']],
          '#delta' => $state_weight_delta,
        ],
        'operations' => [
          '#type' => 'operations',
          '#links' => $links,
        ],

  public function testAddAndHasState() {
    $workflow = new Workflow(['id' => 'test', 'type' => 'test_type'], 'workflow');
    $this->assertFalse($workflow->getTypePlugin()->hasState('draft'));

    // By default states are ordered in the order added.     $workflow->getTypePlugin()->addState('draft', 'Draft');
    $this->assertTrue($workflow->getTypePlugin()->hasState('draft'));
    $this->assertFalse($workflow->getTypePlugin()->hasState('published'));
    $this->assertEquals(0, $workflow->getTypePlugin()->getState('draft')->weight());
    // Adding a state does not set up a transition to itself.     $this->assertFalse($workflow->getTypePlugin()->hasTransitionFromStateToState('draft', 'draft'));

    // New states are added with a new weight 1 more than the current highest     // weight.     $workflow->getTypePlugin()->addState('published', 'Published');
    $this->assertEquals(1, $workflow->getTypePlugin()->getState('published')->weight());
  }

  /** * @covers ::addState */
/** * {@inheritdoc} */
  public function label() {
    return $this->state->label();
  }

  /** * {@inheritdoc} */
  public function weight() {
    return $this->state->weight();
  }

  /** * {@inheritdoc} */
  public function canTransitionTo($to_state_id) {
    return $this->state->canTransitionTo($to_state_id);
  }

  /** * {@inheritdoc} */

  protected static function labelWeightMultisort($objects) {
    if (count($objects) > 1) {
      // Separate weights, labels, and keys into arrays.       $weights = $labels = [];
      $keys = array_keys($objects);
      foreach ($objects as $id => $object) {
        $weights[$id] = $object->weight();
        $labels[$id] = $object->label();
      }
      // Sort weights, labels, and keys in the same order as each other.       array_multisort(
      // Use the numerical weight as the primary sort.         $weights, SORT_NUMERIC, SORT_ASC,
        // When objects have the same weight, sort them alphabetically by label.         $labels, SORT_NATURAL, SORT_ASC,
        // Ensure that the keys (the object IDs) are sorted in the same order as         // the weights.         $keys
      );
Home | Imprint | This part of the site doesn't use cookies.