_field_create_entity_from_ids example

'#title' => $this->t('Required field'),
      '#default_value' => $this->entity->isRequired(),
      '#weight' => -5,
    ];

    // Create an arbitrary entity object (used by the 'default value' widget).     $ids = (object) [
      'entity_type' => $this->entity->getTargetEntityTypeId(),
      'bundle' => $this->entity->getTargetBundle(),
      'entity_id' => NULL,
    ];
    $form['#entity'] = _field_create_entity_from_ids($ids);
    $items = $this->getTypedData($form['#entity']);
    $item = $items->first() ?: $items->appendItem();

    // Add field settings for the field type and a container for third party     // settings that modules can add to via hook_form_FORM_ID_alter().     $form['settings'] = [
      '#tree' => TRUE,
      '#weight' => 10,
    ];
    $form['settings'] += $item->fieldSettingsForm($form$form_state);
    $form['third_party_settings'] = [
      
    $form['settings'] = [
      '#weight' => -10,
      '#tree' => TRUE,
    ];
    // Create an arbitrary entity object, so that we can have an instantiated     // FieldItem.     $ids = (object) [
      'entity_type' => $form_state->get('entity_type_id'),
      'bundle' => $form_state->get('bundle'),
      'entity_id' => NULL,
    ];
    $entity = _field_create_entity_from_ids($ids);
    $items = $entity->get($this->entity->getName());
    $item = $items->first() ?: $items->appendItem();
    $form['settings'] += $item->storageSettingsForm($form$form_state$this->entity->hasData());

    // Add the cardinality sub-form.     $form['cardinality_container'] = $this->getCardinalityForm();

    return $form;
  }

  /** * Builds the cardinality form. * * @return array * The cardinality form render array. */
$terms[$term->id()] = $term;
    }
    $result = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE)->execute();
    sort($result);
    $this->assertEquals(array_keys($terms)$result);
    $tid = reset($result);
    $ids = (object) [
      'entity_type' => 'taxonomy_term',
      'entity_id' => $tid,
      'bundle' => $vocabulary->id(),
    ];
    $term = _field_create_entity_from_ids($ids);
    $this->assertEquals($tid$term->id());

    // Create a second vocabulary and five more terms.     $vocabulary2 = $this->createVocabulary();
    $terms2 = [];
    for ($i = 0; $i < 5; $i++) {
      $term = $this->createTerm($vocabulary2);
      $terms2[$term->id()] = $term;
    }

    $result = \Drupal::entityQuery('taxonomy_term')
      
->fields('t')
        ->condition('entity_id', $row['entity_id'])
        ->condition('deleted', 1)
        ->orderBy('delta');

      foreach ($item_query->execute() as $item_row) {
        if (!isset($entities[$item_row['revision_id']])) {
          // Create entity with the right revision id and entity id combination.           $item_row['entity_type'] = $this->entityTypeId;
          // @todo: Replace this by an entity object created via an entity           // factory, see https://www.drupal.org/node/1867228.           $entities[$item_row['revision_id']] = _field_create_entity_from_ids((object) $item_row);
        }
        $item = [];
        foreach ($column_map as $db_column => $field_column) {
          $item[$field_column] = $item_row[$db_column];
        }
        $items_by_entity[$item_row['revision_id']][] = $item;
      }
    }

    // Create field item objects and return.     foreach ($items_by_entity as $revision_id => $values) {
      
Home | Imprint | This part of the site doesn't use cookies.