dx.new » d8 » snippets » x_1

Field storage, Field config, Display mode (form, view)

  public static function __ensureFieldStorageConfig() {
    $storage = self::getStorage('field_storage_config');
    $entity_type_id = 'slog_target_term';
    $fields = $storage->loadByProperties(['entity_type' => $entity_type_id]);
    if (empty($fields)) {
      $field_storage_values = [
          'field_name' => "slogtx_status",
          'entity_type' => $entity_type_id,
          'type' => 'boolean',
          'translatable' => TRUE,
          'persist_with_no_fields' => TRUE,
      ];
      $storage->create($field_storage_values)->save();
    }
  }
 
  public static function __cleanupFieldStorageConfig() {
    $fields = self::getStorage('field_storage_config')
            ->loadByProperties(['entity_type' => 'slog_target_term']);
    foreach ($fields as $field) {
      $field->delete();
    }
  }
 
  private function __addTargetTermStatusField() {
    try {
      $entityTypeManager = \Drupal::entityTypeManager();
 
      // Create the field field.slog_target_term.slogtx_status
      $bundle = $this->id();
      $field_name = 'slogtx_status';
      $field_label = t('Enable target term');
      $entityTypeId = 'slog_target_term';
      $field_values = array(
          'field_name' => $field_name,
          'entity_type' => $entityTypeId,
          'bundle' => $bundle,
          'label' => $field_label,
          'required' => TRUE,
      );
      $entityTypeManager
              ->getStorage('field_config')
              ->create($field_values)
              ->setDefaultValue(TRUE)
              ->save();
 
 
      // Create Entity Form Display and Entity View Display
      foreach (['entity_form_display', 'entity_view_display'] as $display_type) {
        $values = array(
            'targetEntityType' => $entityTypeId,
            'bundle' => $bundle,
            'mode' => 'default',
            'status' => TRUE,
        );
 
        //todo::now::now - \Drupal\Core\Entity\EntityDisplayBase
        //schade zu wegwerfen, vielleicht nach .../snippets
        $options = array();
        if ($display_type == 'entity_form_display') {
          $options = array(
              'settings' => array('display_label' => TRUE),
              'third_party_settings' => array(),
              'type' => 'boolean_checkbox',
          );
        }
        $entityTypeManager
                ->getStorage($display_type)
                ->create($values)
                ->setComponent($field_name, $options)
                ->save();
      }
    } catch (\Exception $e) {
      $args = array('%label' => $field_label, '@message' => $e->getMessage());
      $msg = t('There was a problem creating field %label: @message', $args);
      drupal_set_message($msg, 'error');
      SlogTaxonomy::logger()->error($msg);
    }
  }
 
  public function __deleteTargetTermStatusField() {
    $entityTypeManager = \Drupal::entityTypeManager();
    $entityTypeId = 'slog_target_term';
    $bundle = $this->id();
 
    // Delete Field Config
    $id = "$entityTypeId.$bundle.slogtx_status";
    $field_config = $entityTypeManager->getStorage('field_config')->load($id);
    if ($field_config) {
      $field_config->delete();
    }
 
    // Delete Entity Form Display and Entity View Display
    $id = "$entityTypeId.$bundle.default";
    foreach (['entity_form_display', 'entity_view_display'] as $display_type) {
      $display = $entityTypeManager->getStorage($display_type)->load($id);
      if ($display) {
        $display->delete();
      }
    }
  }  
d8/snippets/x_1.txt · Zuletzt geändert: 2022/06/06 15:13 von 127.0.0.1
Recent changes RSS feed Debian Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki