Set label of $entity programmatically in a custom form I need to change the label of a custom content type programmatically. I've looked around but I couldn't find a solution. This is my custom form:

class GroupContentForm extends ContentEntityForm {

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $entity = $this-&gt;entity;

    $entity-&gt;set('label', 'kkkkk');
    $status = parent::save($form, $form_state);

    $form_state-&gt;setRedirect('entity.group_content.canonical', ['group_content' =&gt; $entity-&gt;id()]);
  }

}


What do I need to do to change the label? $entity-&gt;set('label', 'kkkkk'); causes an error as the field label doesn't exists, the same happens with title and _title
It's not clear what entity type you have. In the question you ask for the label of a content type, which would be the entity node, then the label field is title. In the question title it is a more general question about the label of an entity and in the code you redirect to a group_content entity route, which would suggest this is another entity type.

In this case, if you don't know the entity type, you could use this generic code to get the label field name and set the label with it:

$label_field = $entity-&gt;getEntityType()-&gt;getKey('label');
$entity-&gt;set($label_field, 'New label');