Jump to content
  • Edit CMS record


    For 3 hours, I struggled with editing CMS records of different databases, each with many custom fields.

    Each time I try, I get a template error. Anyways, the problem was with the next function in record.php:

    protected function sendQuoteAndMentionNotifications( array $exclude=array() ): array
    {
    $data = array( 'quotes' => array(), 'mentions' => array(), 'embeds' => array() );
    
    $className = 'IPS\cms\Fields' .  static::$customDatabaseId;
    /* @var $className Fields */
    foreach ( $className::data() as $field )
    {
    if ( $field->type == 'Editor' )
    {
    $key = "field_{$field->id}";
    
    $_data = static::_getQuoteAndMentionIdsFromContent( $this->$key );
    foreach ( $_data as $type => $memberIds )
    {
    $_data[ $type ] = array_filter( $memberIds, function( $memberId ) use ( $field )
    {
    return $field->can( 'view', Member::load( $memberId ) );
    } );
    }
    
    $data = array_map( 'array_unique', array_merge_recursive( $data, $_data ) );
    }
    }
    
    return $this->_sendQuoteAndMentionNotifications( $data, $exclude );
    }

    Replacing it with this one will solve the issue:
     

    protected function sendQuoteAndMentionNotifications(array $exclude = array()): array
    {
    $data = array('quotes' => array(), 'mentions' => array(), 'embeds' => array());
    
    $className = 'IPS\cms\Fields' . static::$customDatabaseId;
    /* @var $className Fields */
    foreach ($className::data() as $field) {
    if ($field->type == 'Editor') {
    $key = "field_{$field->id}";
    
    // Check if $this->$key is not null and is a string
    if (!empty($this->$key) && is_string($this->$key)) {
    $_data = static::_getQuoteAndMentionIdsFromContent($this->$key);
    
    foreach ($_data as $type => $memberIds) {
    $_data[$type] = array_filter($memberIds, function ($memberId) use ($field) {
    return $field->can('view', Member::load($memberId));
    });
    }
    
    $data = array_map('array_unique', array_merge_recursive($data, $_data));
    } else {
    \IPS\Log::log("Field content is empty or invalid for key: {$key}", 'invalid_field_content');
    }
    }
    }
    
    return $this->_sendQuoteAndMentionNotifications($data, $exclude);
    }

    Changes Made:

    1. Check for Valid Content: Before calling _getQuoteAndMentionIdsFromContent, the code now checks if $this->$key is not empty and is a string (if (!empty($this->$key) && is_string($this->$key))). This prevents passing null to the method.

    2. Logging for Invalid Content: If the content is null or not a string, it logs a message to help with debugging.

    I hope this will be useful and fixed in the next release.

    Notice: I don't know if this is the right way to fix the bug, but it works.


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...