8 – How to delete a field programmatically

I’ve got a service managing the install/uninstall of my module.

Install create a field attached to the user but I am not able to delete it during the uninstall process…

stacko.services.yml

services:
  stacko.module_installer:
    class: DrupalstackoModuleInstaller
    arguments:
      - '@entity_type.manager'
    tags:
      - { name: service_collector, tag: 'module_install.uninstall_validator', call: addUninstallValidator }

stacko.install

/**
 * Implements hook_install().
 */
function stacko_install() {
  $module_installer = Drupal::service('stacko.module_installer');
  $module_installer->install(());
}

/**
 * Implements hook_uninstall().
 */
function stacko_uninstall() {
  $module_installer = Drupal::service('stacko.module_installer');
  $module_installer->uninstall(());
}

src/ModuleInstaller.php

namespace Drupalstacko;

use DrupalCoreEntityEntityTypeManagerInterface;
use DrupalCoreExtensionModuleInstallerInterface;
use DrupalCoreExtensionModuleUninstallValidatorInterface;
use DrupalfieldEntityFieldStorageConfig;

/**
 * Module installer.
 */
class ModuleInstaller implements ModuleInstallerInterface {

  /**
   * Field storage config manager.
   *
   * @var DrupalfieldFieldStorageConfigStorage
   */
  protected $fieldStorageConfigManager;

  /**
   * Field config manager.
   *
   * @var DrupalfieldFieldConfigInterface
   */
  protected $fieldConfigManager;

  /**
   * Constructor.
   *
   * @param DrupalCoreEntityEntityTypeManagerInterface $entity_type_manager
   *   The entityt type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->fieldStorageConfigManager = $entity_type_manager->getStorage('field_storage_config');
    $this->fieldConfigManager = $entity_type_manager->getStorage('field_config');
  }

  /**
   * {@inheritdoc}
   */
  public function install(array $module_list, $enable_dependencies = TRUE) {
    $this->createFields();
  }

  /**
   * {@inheritdoc}
   */
  public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
    $this->deleteFields();
  }

  /**
   * {@inheritdoc}
   */
  public function addUninstallValidator(ModuleUninstallValidatorInterface $uninstall_validator) {
  }

  /**
   * {@inheritdoc}
   */
  public function validateUninstall(array $module_list) {
  }

  /**
   * Create all fields.
   */
  protected function createFields() {
    $this->createStackoField();
  }

  /**
   * Create Stacko field.
   */
  protected function createStackoField() {
    $this->createStackoFieldStorage();
    $this->createStackoFieldInstance();
  }

  /**
   * Delete all fields.
   */
  protected function deleteFields() {
    $this->deleteStackoField();
  }

  /**
   * Create Stacko field storage.
   */
  protected function createStackoFieldStorage() {
    try {
      $this->fieldStorageConfigManager->create((
        'field_name' => 'field_stacko',
        'entity_type' => 'user',
        'type' => 'text',
        'cardinality' => 1,
        'locked' => FALSE,
        'indexes' => (),
        'settings' => (
          'max_length' => 14,
          'is_ascii' => false,
          'case_sensitive' => false,
        ),
      ))->save();
    }
    catch (Exception $e) {
      if (PHP_SAPI === 'cli') {
        drush_print_r('Either the field_stacko storage already exists or an error occured and it has not been created.');
      }
    }
  }

  /**
   * Create Stacko field instance.
   */
  protected function createStackoFieldInstance() {
    try {
      $this->fieldConfigManager->create((
        'field_name' => 'field_stacko',
        'entity_type' => 'user',
        'bundle' => 'user',
        'label' => 'The stacko',
        'required' => FALSE,
        'settings' => (),
      ))->save();
    }
    catch (Exception $e) {
      if (PHP_SAPI === 'cli') {
        drush_print_r('Either the field_stacko instance already exists or an error occured and it has not been created.');
      }
    }
  }

  /**
   * Delete Stacko field storage.
   */
  protected function deleteStackoField() {
    // Leads to Error: Call to undefined method DrupalfieldFieldStorageConfigStorage::loadByName()
    /* if ($this->fieldStorageConfigManager->loadByName('user', 'field_stacko')) {
      $field_storage->delete();
    } */
    // Doesn't load the field storage | doesn't delete the field.
    /* if ($field_storage = $this->fieldStorageConfigManager->loadByProperties((
      'entity_type' => 'user',
      'field_name' => 'field_stacko',
    ))) {
      kint('test 1');
      $field_storage->delete();
    }
    kint('test 2'); */
    // Doesn't load the field storage | doesn't delete the field.
    if ($field_storage = FieldStorageConfig::loadByName('user', 'field_stacko')) {
      kint('test 1');
      $field_storage->delete();
    }
    kint('test 2');
  }

}

While running deleteStackoField() I only have the ‘test 2’ message. As it’s unable to load the field storage, the field is not deleted.

So I am wondering how can I properly delete my field programmatically ?

graphics – Keynote Mac, delete cropped image to reduce file size without losing image resolution quality

I have lots of high-resolution images inserted into the Keynote which makes the file very large. Image masking in Keynote is very convenient for me. But I only want a small corner of the images shown in the slides, and I don’t need the cropped part anymore. Reducing file size under the File menu loses image quality and the cropped images are still there. Is there any way to delete the cropped part to reduce file size without losing image resolution/quality?

macos – How to delete a file from Time Machine backups in Big Sur?

I can’t seem to take a screenshot of Time Machine, but I’ll describe it. I go to the Time Machine menu and select “Enter Time Machine”. Then I see the view with the stack of windows. I browse to a file and select it. Now I want to delete it so it doesn’t exist and take up space in backups. There is no option to delete it – either the action menu in the toolbar, or the control-click context menu.

How do I delete a file from Time Machine backups? There used to be a way to do this.

nat – WireGuard: cannot delete iptables rule for default route

since I don’t want friends and colleagues in my VPN to use my VPN server as a proxy VPN for “anonymous” surfing, I want to disable the default route for the VPN. In a nutshell:

  • LAN (10.20.0.0/24) must be accessible
  • WAN (0.0.0.0/0) must be inaccessible

I was unable to find a WireGuard setting to do this except configuring the AllowedIPs directive in the client config. But what kind of security does that provide?? Anyone can easily edit his/her config, replace 10.20.0.0/24 with 0.0.0.0/0, and use my VPN as a proxy…

My next approach was to delete the iptables rule that permitts the forwarding from the VPN subnet to the WAN. But somehow I cannot delete the affected rule. If I create a similar rule (same subnet, same policy) I can delete it, but I am prevented from deleting the WireGuard rule somehow.

The rule in question has been marked with --> in the following output:

root@(...):~# iptables -L FORWARD

    Chain FORWARD (policy DROP)
    target     prot opt source               destination
    ...
    ACCEPT     all  --  anywhere             10.6.0.0/24          ctstate RELATED,ESTABLISHED /* wireguard-forward-rule */
--> ACCEPT     all  --  10.6.0.0/24          anywhere             /* wireguard-forward-rule */

Commands that I have tried to get rid of this rule:

root@(...):~# iptables -D FORWARD -s 10.6.0.0/24 -j ACCEPT
iptables: Bad rule (does a matching rule exist in that chain?).

If I add the same rule again (without the comment):

root@(...):~# iptables -L FORWARD

    Chain FORWARD (policy DROP)
    target     prot opt source               destination
    ...
    ACCEPT     all  --  anywhere             10.6.0.0/24          ctstate RELATED,ESTABLISHED /* wireguard-forward-rule */
--> ACCEPT     all  --  10.6.0.0/24          anywhere             /* wireguard-forward-rule */
--> ACCEPT     all  --  10.6.0.0/24          anywhere

root@(...):~# iptables -D FORWARD -s 10.6.0.0/24 -j ACCEPT
root@(...):~#

No problem… 😐

Note: If you need further logs/output, please let me know. Thanks in advance!


FINAL ANSWER:
WireGuard also specifies interfaces and a comment. These have to be an exact match when deleting rules. You can see the full list of arguments using iptables with the -v option.

The command that finally removed the rule was:

iptables -D FORWARD -i wg0 -o wlan0 -s 10.6.0.0/24 -m comment --comment "wireguard-forward-rule" -j ACCEPT

How to delete all but certain values from many JSON files?

i’ve used another Terminal utility to download all my videos from Youtube (youtube-dl if interested). with these, are created a JSON file for each video file. inside these JSON files is a LOT of junk. but there are 3 items/values/categories(?) that i want to keep, the rest can be deleted.

those things are
{“upload_date”: “value”,
“fulltitle”: “value”,
“description”: “value”}

so i either need to:

  1. delete all but these 3 items & save the file, or
  2. extract these 3 items to a new file & delete the old one.

hoping for (but haven’t been able to find) a Mac app that can batch do this for all the files at once.
or
have a Terminal script (that’s EASY, as i don’t know about all that),
or
an Automator script (again, that’s EASY) that’ll do it all.

google cloud platform – GCP SLQ instance wont stop ,restart, or delete

The status says “under maintenance” and has for several hours. This is a not a large database as I am a new student with very small tables for learning. I have tried stopping and restarting as well as deleting the instance, but get an error message every time. The error simply says “The attempted action failed, please try again. Request ID: 4022803414404737811”. Operations and logs shows an “update” error “unknown error” this morning, but no other information given.

I have logged out of GCP and back in with no effect.

boot – how to delete a partition(sdb6) that is ordered before the root partition (sdb7)

Recently I has been struggling with updating from Ubuntu 18.04 to Ubuntu 2.04, and I ended up installing a new os. now I have my old root partition as /dev/sdb6, and I formatted it, and I want to use it to extend the current root partition (/dev/sdb7). but it’s hard as it’s before the current root in order. Any help how can I do this ? enter image description here

maildir – Dovecot: is there a way to delete specific email from ALL mailboxes of one domain?

I want to be able to delete specific email message from all mailboxes of one domain, does Dovecot has this type of functionality OR are there any known good solution for this? It’s Maildir format.

The problem

many disposable, one-shot emails, often with multi-megabyte attachments.

What have I tried

I have looked to doveadm Batch, Altmove, Expunge commands, none of which seems to do the (exactly this type of) job?

What I’d like to avoid if possible

The last resort: grep recursively by either Message-ID header or ESMTP id part of one of the multiple Received headers.

sql server – SQL Trigger question (update and delete)

There are 3 tables, bookstore, book, and time log,

  1. How would I create an update trigger to record/insert the price into the price_logs table before updating the book table? Also record the user who updated the price

  2. How would I create a delete trigger to delete a record from bookstore table when a book is deleted

enter image description here